Android: CountDownTimer skips last onTick()!

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

    You are calculating time remaining incorrectly. The callback gets the number of milliseconds until completion of the task.

    public void onTick(long m) {  
        long sec = m/1000+1;  
        tv.append(sec+" seconds remain\n");  
    }  
    

    should be

    public void onTick(long m) {  
        long sec = m/1000;  
        tv.append(sec+" seconds remain\n");  
    }
    

    I've never used this class myself but it looks like you will not get a callback the instant it starts, which is why it appears like you're missing an entry. e.g. 10000 ms, 1000 ms per tick you'd get a total of 9 update callbacks, not 10 - 9000, 8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000, finish.

提交回复
热议问题