Android: CountDownTimer skips last onTick()!

前端 未结 12 690
眼角桃花
眼角桃花 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:01

    While the solution above is valid, it can be further improved. It unnecessarily has a runnable inside another class (which can already be treated on it's own). So just create a class that extends a thread (or runnable).

        class MyTimer extends Thread {
          private long millisInFuture;
          private long countDownInterval;
          final Handler mHandler = new Handler();
    
          public MyTimer(long pMillisInFuture, long pCountDownInterval) {
            this.millisInFuture = pMillisInFuture;
            this.countDownInterval = pCountDownInterval;
          }
    
          public void run() {
            if(millisInFuture <= 0) {
              Log.v("status", "done");
            } else {
              millisInFuture -= countDownInterval;
              mHandler.postDelayed(this, countDownInterval);
            }
          }
        }
    

提交回复
热议问题