Skip to content Skip to sidebar Skip to footer

How Can I Pause One Countdown Timer When Another One Is Running?

I have two CountDownTimers in my program: a 4 second one, and 24 second one. I want the longer timer to be paused for every 4 seconds the shorter timer is running. Then when the sh

Solution 1:

Create separate methods to start/pause/resume 24 second timer in your code. You need to store Your milliTillFinished of 24 seconds timer so you can pass it to resume timer method. You can pause 24 second timer on onTick() method of 4 second timer and resume it on onFinish() method.

To start timer:

publicvoidtimerStart(long timeLengthMilli) {
    timer = newCountDownTimer(timeLengthMilli, 1000) {

        @OverridepublicvoidonTick(long milliTillFinish) {
            milliLeft=milliTillFinish;
        }

To pause timer :

publicvoidtimerPause() {
    timer.cancel();
}

To resume timer:

privatevoidtimerResume() {
    timerStart(milliLeft);
}

Post a Comment for "How Can I Pause One Countdown Timer When Another One Is Running?"