Search code examples
androidtimercountdown

Why is my milliseconds count down not consistent?


For some reason my timer does not count down consistantly, it sometimes goes back to the previous second or so.

For example: if I begin the timer at 49 seconds this is whats printed out:

(in milliseconds)

48774
47374
48909 //inconsistent

47063
46212
44987
48426 //inconsistent

46294 //inconsistent

44738
43636
42410
...and so on

All I do it pass in two integers (minutes and seconds) and combine them together to get the total number of milliseconds.

Here is my code:

new CountDownTimer(((min * 60 + sec) * 1000), 1000) {//total time, interval

                public void onTick(long millisUntilFinished) {
                    System.out.println(millisUntilFinished);
                //...
                }
}

How can I get the milliseconds to display consistently?

EDIT:

I have also tried adding this to onTick but it still does not work

if((int)prevMill > (int)millisUntilFinished){
        prevMill = millisUntilFinished;
        System.out.println(prevMill);
 }

Solution

  • I found that onTick() is calculated from the time the previous onTick() ran, which creates a tiny error on every tick.

    See this post: android CountDownTimer - additional milliseconds delay between ticks