Android: CountDownTimer skips last onTick()!

前端 未结 12 697
眼角桃花
眼角桃花 2020-11-27 14:51

Code:

public class SMH extends Activity {  

    public void onCreate(Bundle b) {  
        super.onCreate(b);  
        setContentView(R.layou         


        
12条回答
  •  天涯浪人
    2020-11-27 15:21

    The most simple solution I came up with is as follows. Note that it only works if you need a simple screen to display with a seconds countdown.

    mTimer = new CountDownTimer(5000, 100){
                public void onTick(long millisUntilFinished) {
                    mTimerView.setText(Long.toString(millisUntilFinished/1000));                
                 }
    
                 public void onFinish() {
                     mTimerView.setText("Expired");
                 }
            };
    
            mTimer.start();
    

    In the code above the onTick() is called every 100 milliseconds but visually only seconds are displayed.

提交回复
热议问题