1

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);
 }
bob dylan
  • 647
  • 1
  • 10
  • 25
  • 1
    Because, in general, the system clock is only accurate +/- 15ms. You could round to the nearest 15, and it should be consistent. – Elliott Frisch Feb 28 '16 at 05:33
  • But there must be some way to make it display correctly, I mean our phones already have timers that countdown without any inconsistencies. – bob dylan Feb 28 '16 at 05:35

2 Answers2

3

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

Community
  • 1
  • 1
fman
  • 230
  • 2
  • 10
  • This would explain the interval not being exact, but not the increasing value of millisUntilFinished. My best guess is that Dylan accidentally starts multiple countdowns. – Snild Dolkow Feb 28 '16 at 06:11
0

Even though you specify values to the millisecond in APIs like that, by default the system timer is only going to have about +/-15ms resolution (varies by OS, etc).

This can be changed, see here: https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/

modal_dialog
  • 733
  • 5
  • 12