I am using countDownTimer in my code. The problem is, countdowntimer is not accurate. The onTick method will not always execute every second, it can be a few milliseconds off. This becomes an issue when trying to execute a certain task based on millisUntilFinished. I am trying to log when ten seconds of my 20 second timer has passed:
Here is the relevant code:
if (millisUntilFinished == 10000) { //TEN SECONDS
Log.v(TAG, "TEN SECONDS LEFT");
}
The issue arises here, as countDownTimer may never even have millisUntilFinished == 10000, it may equal 1001 and so my code will not execute. Is there anyway to somehow round the millisUntilFinished (which is a long) to the nearest thousands? I have tried the following:
millisUntilFinishedRounded = MathUtils.round((double) millisUntilFinished, -3); // nearest thousand, 2000.0
but I cannot resolve 'MathUtils'
I am lost. I would really appreciate any feedback (positive or negative)! Thank you so much for all of your help.
{Rich}
((500 + millisUntilFinished) / 1000) * 1000rounds a positive value to the nearest 1000. But you could use(millisUntilFinished >= 9500 && millisUntilFinished < 10500)as your condition alternatively. - Andy Turner(750 + 500) / 1000 * 1000 = 1000, whereas750 / 1000 * 1000 = 0. - Andy Turner