Android: CountDownTimer skips last onTick()!

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

    I found easy solution. I need CountDown to update ProgressBar, so I did this:

    new CountDownTimer(1000, 100) {
    
        private int counter = 0;
    
        @Override
        public void onTick(long millisUntilFinished) {
            Log.d(LOG_TAG, "Tick: " + millisUntilFinished);
            if (++counter == 10) {
                timeBar.setProgress(--lenght); // timeBar and lenght defined in calling code
                counter = 0;
            }
        }
    
    
        @Override
        public void onFinish() {
            Log.d(LOG_TAG, "Finish.");
    
            timeBar.setProgress(0);
        }
    
    };
    

    Small tick do the trick :)

提交回复
热议问题