S
S
Sasha Brahms2016-08-01 14:02:24
Android
Sasha Brahms, 2016-08-01 14:02:24

Problems with CountDownTimer?

Hello everyone, I'm trying to figure out the timer, but it works somehow strange, or I don't understand (rather it)

public class MyTimer extends CountDownTimer{
    int current_prg = 80;
    public GameTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long l) {
        current_prg--;
        Log.d("TAG", "TICK "+l+" : "+current_prg);
    }

    @Override
    public void onFinish() {
        Log.d("TAG", "FINISH : "+current_prg);
    }
}

I run:
MyTimer timer = new MyTimer(8000,100);// каждые 100 миллисекунд в течении 8 секунд (80 раз) 
timer.start();

___
In theory, as I understand it, onTick will be executed every 100 milliseconds, as 8 seconds pass, onFinish will be executed
And in my case, current_prg should be equal to 0, but it is equal to when 21, when 30, why?
I don't understand...
Also, onTick(long l) - I thought l would be a multiple of 100, for example 7900,7800,7700, etc., but in reality it goes something like this:
7880, 4999.4897, etc. (taken from the log)
Please explain, Why is that, I can't figure it out...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2016-08-01
@petermzg

Since you do not have a real-time system and therefore android switches between tasks (launched applications). As the opportunity arose, so did your method.
You are not guaranteed that it will execute in exactly 100ms, but it is guaranteed that it will not execute before 100ms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question