1

I'd like to pause countdown timer in specific condition. But in my code, once it starts, it never stop even when mIntheGym is false. Please help me on this issue. Thanks in advance.

Here is my code below.

    TimerTask doAsynchronousTask1 = new TimerTask() {
        @Override
        public void run() {
            handler1.post(new Runnable() {
                public void run() {
                    try {
                        if (mIntheGym) {
                            mtextview.setText("counter starts");
                            mCountDownTimer1 = new CountDownTimer(mTimeLeftInMillis1, 500) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    mTimeLeftInMillis1 = millisUntilFinished;
                                    updateCountDownText();
                                }

                                @Override
                                public void onFinish() {
                                }
                            }.start();
                        }
                        else {
                            mtextview.setText("stop counter");
                            if (mCountDownTimer1 != null) {
                                mCountDownTimer1.cancel();
                            }
                        }
                    } catch (Exception e) {
                    }
                }
            });
        }
    };
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

You cannot pause a TimerTask. Once it is canceled, you cannot run it again.

You can look here for more information : Cancel a TimerTask

Hope it helps!

Maxouille
  • 2,729
  • 2
  • 19
  • 42