I have a Countdowntimer object. Basically the timer is supposed to countdown from 10 to 1. What it does is starts the timer from 9 and it ends on 1 but it stays on 1 for about 2 seconds... Why does this happen. I've made sure that my timer is not overlapping/getting called twice in a row as that could be the cause. This is part of my timer object.
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.d("timer","Actual: " + Long.toString(millisUntilFinished)+".... "+Long.toString(millisUntilFinished/1000));
time.setText(Long.toString(millisUntilFinished / 1000));
}
This is what I am getting from the Log.d outputs:
03-03 21:23:15.438 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9964.... 9
03-03 21:23:16.453 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8949.... 8
03-03 21:23:17.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7939.... 7
03-03 21:23:18.473 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6929.... 6
03-03 21:23:19.482 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5919.... 5
03-03 21:23:20.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4909.... 4
03-03 21:23:21.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3899.... 3
03-03 21:23:22.513 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2889.... 2
03-03 21:23:23.523 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1879.... 1
03-03 21:23:25.436 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9978.... 9
03-03 21:23:26.453 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8961.... 8
03-03 21:23:27.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7951.... 7
03-03 21:23:28.473 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6941.... 6
03-03 21:23:29.483 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5931.... 5
03-03 21:23:30.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4921.... 4
03-03 21:23:31.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3911.... 3
03-03 21:23:32.514 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2900.... 2
03-03 21:23:33.522 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1891.... 1
03-03 21:23:35.431 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9994.... 9
03-03 21:23:36.443 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 8982.... 8
03-03 21:23:37.454 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 7971.... 7
03-03 21:23:38.463 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 6961.... 6
03-03 21:23:39.474 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 5951.... 5
03-03 21:23:40.493 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 4932.... 4
03-03 21:23:41.503 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 3922.... 3
03-03 21:23:42.513 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 2912.... 2
03-03 21:23:43.523 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 1902.... 1
03-03 21:23:45.456 1947-1947/com.w1481217.braintraninggame D/timer: Actual: 9978.... 9
It seems that the ticks don't come in at perfectly precise intervals as maybe expected. In your case, the value of
millisUntilFinished
in consecutiveonTick
callbacks isn't guaranteed to be 10000, 9000, ..., 0.The first tick doesn't have 9 seconds (or 9000ms) remaining, it has 9964ms. So, only 36ms has passed from the time you called
start()
on theCountDownTimer
to the time of this first tick callback. Chris' comment on his answer that the "first callback happens after 1000ms" seems to be incorrect (I'd make a comment on his answer, but I don't have the rep!). You're seeing 9 in your text view and log statement because of the classic integer division problem. You're not seeing the additional 0.964 seconds because the result of the division is of typelong
, which does not have a fractional part. Try using1000.0
as your divisor in the log statement and remove theLong.toString()
and see what value is shown.In regards to the hanging towards the end of the countdown, see the following: https://stackoverflow.com/a/12283400/1244019
Depending on how precise you want your timer, perhaps a solution to your problem would be to keep a counter within your
CountDownTimer
, updating your TextView and decrementing the counter each timeonTick
is called. See the following:The reason is because the time interval isn't fine enough and callback isn't at exactly quantas of 10,000
This code in Kotlin is presenting the remaining time punctuately: