2

I'm having android countdown timer which is starting automatically. I need to pause and resume it. I've seen other solutions but they didn't work. My code is:

class Timer extends CountDownTimer
{
    public Timer(long millisInFuture, long countDownInterval)
    {
        super(millisInFuture, countDownInterval);
        timerTimedOut = false;
    }

    @Override
    public void onFinish() {
        if(timerTimedOut){
            doSTH();
        } else {
            doSTHElse();
        }

        this.start();
    }

    @Override
    public void onTick(long millisUntilFinished)
    {
        timerShow.setText(Long.toString(millisUntilFinished / 1000));

    }
    public void stop(){
        timerTimedOut = true;
        this.cancel();
        onFinish();
    }
}

What should I do to pause and resume it?

Petar Toshev
  • 67
  • 1
  • 1
  • 9

1 Answers1

3

I have been dealing with this and after trying many things, I found this solution. Here you can find a great alternative for the Android CountDownTimer class: https://gist.github.com/bverc/1492672

You just have to create a new class named CountDownTimer2, and paste that code. Then, use it instead of the normal CountDownTimer class, for example:

 CountDownTimer2 cdt = new CountDownTimer2(30000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            //Whatever you want to do onTick


        }

        @Override
        public void onFinish() {
            Log.i(TAG, "Timer finished");
        }


    };

    cdt.start();

}

An then you just need to pass

cdt.pause();

or

cdt.resume();

Hope it helps.

algarrobo
  • 304
  • 1
  • 12