Stephan
Stephan

Reputation: 527

Android two CountDownTimers executing one after the other

I have two countdowntimers in my activity. One set to countdown from 10 secondes the other from 5 seconds. When I click the countdownbutton, timer 1 starts. When this timer finishes, I want timer2 to start, the problem is that timer2 never gets excuted.

I have 2 local var which track what timer is executing, when I debug, I see that the right value for these vars is set, but still the secondtimer is not executing.

Here is my CountdownTimer-class:

 public class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        if (ctHasStarted) {
            ctHasStarted = false;
            timerValue.setText("00:00:000");

            countDownTimer2.start();
            ct2HasStarted = true;
        }
        if (ct2HasStarted) {
            ct2HasStarted = false;
            timerValue2.setText("00:00:000");

            countDownTimer.start();
            ctHasStarted = true;
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {

        int secs = (int) (millisUntilFinished / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (millisUntilFinished % 1000);

        if (ctHasStarted)
            timerValue.setText(String.format("%02d",mins) + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        if (ct2HasStarted)
            timerValue2.setText(String.format("%02d",mins) + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));

    }
}

ctHasStarted and ct2HasStarted are boolean values to keep track of what timer is running. As said when I debug, the lines countDownTimer.start() and countDownTimer2.start() both are reached. But the textfield for timer2 never gets updated. even the line timerValue2.setText("00:"00:00") doesn't work.

I initialize both timers in my onCreate of the activity like:

countDownTimer = new MyCountDownTimer(ctStartTime, interval);
countDownTimer2 = new MyCountDownTimer(ct2StartTime, interval);

An other strange thing I noticed is that when I change line if (ct2HasStarted) in the onTick-event to else the timerValue2.setText-is showing a countdown. But after the second times is finished, the first one is not executed despite the call for start of the first time in the onFinish event.

I think I miss something, but I don't know what I miss. Any suggestions?

Upvotes: 0

Views: 112

Answers (1)

Stephan
Stephan

Reputation: 527

Oke, I found it. I refactored my code a little, but most important was adding return in the onFinish section.

I added a new function in my activity which sets some vars. like so:

 public void startTimer(int counterId){
    CountDownTimer counter = null;
    if (counterId == 1) {
        ctHasStarted = true;
        timerValue2.setText("00:00:000");
        timerValue.setText("00:00:000");
        ct2HasStarted = false;

        counter = new MyCountDownTimer(ctStartTime, interval);
    }
    if (counterId == 2) {
        ct2HasStarted = true;
        timerValue2.setText("00:00:000");
        timerValue.setText("00:00:000");
        ctHasStarted = false;
        counter = new MyCountDownTimer(ct2StartTime, interval);
    }

    if(counter !=null ){
        counter.start();
    }
}

And I changed my onFinish event in my counter to:

  @Override
    public void onFinish() {
        if (ctHasStarted) {
            startTimer(2);
            return;
        }
        if (ct2HasStarted) {
            startTimer(1);
            return;
        }
    }

That works. (for me at least).

Upvotes: 0

Related Questions