Search code examples
javaandroidcountdowncountdowntimercontinue

Continue countdown timer after pause


I have a countdown timer, I have a button that pause it, but I need that when you click on button, continue to countdown. I search but couldn't a function related this. How can do it? This is my code, I only managed to restart it, but not continue:

private TextView cuentaRegresiva;
private Button btnEmpezar;
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private long startTime = 30 * 1000;
private final long interval = 1 * 1000;
private long restante;



@Override
protected void onCreate(Bundle savedInstanceState) {
...

btnEmpezar.setOnClickListener(iniciar);

 }

OnClickListener iniciar=new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if (!timerHasStarted && !pausado) {
               countDownTimer.start();
               timerHasStarted = true;
               btnEmpezar.setText("Pause");
               pausado=false;
                }
        else if(timerHasStarted && !pausado){
               countDownTimer.cancel();
               timerHasStarted = false;
               btnEmpezar.setText("Restart");
               pausado=true;
              }
        else if(!timerHasStarted && pausado){
            countDownTimer2.start();
            timerHasStarted = true;
            btnEmpezar.setText("Pause");
            pausado=false;
        }
    }
};

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

      }

      @Override
      public void onFinish() {
          cuentaRegresiva.setText("Tiempo!");
      }

      @Override
      public void onTick(long millisUntilFinished) {
          cuentaRegresiva.setText("" + millisUntilFinished / 1000);
      }
     }

public class MyCountDownTimer2 extends CountDownTimer {
          public MyCountDownTimer2(long restante, long interval) {
           super(restante, interval);

          }

          @Override
          public void onFinish() {
              cuentaRegresiva.setText("Tiempo!");
          }

          @Override
          public void onTick(long millisUntilFinished) {
              cuentaRegresiva.setText("" + millisUntilFinished / 1000);

          }
         }

I thought about taking millisUntilFinished to a variable, but didn't work. Anyway I guess the way is close to that.


Solution

  • You can try saving the seconds until finish, and then you can start the new countdown timer with that seconds.

    // -----------------------

    Cuando presionas el boton de pausa, guarda los segundos que le faltan al timer para que termine. Entonces, cuando volves a apretar play, creas un nuevo CountDownTimer con esos segundos que te faltaban.

    UPDATE

    I did an example:

    public class MainActivity extends Activity {
        private static final int TIMER_TIME = 10000; // in millis
        private Button btnCountdown;
        private TextView tvTimeUntilFinish;
        private boolean mIsPaused = true;
        private long mMillisUntilFinish;
        private CountDownTimer mTimer;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mMillisUntilFinish = TIMER_TIME;
    
            btnCountdown = (Button) findViewById(R.id.btnCountdown);
            tvTimeUntilFinish = (TextView) findViewById(R.id.tvTimeUntilFinish);
    
            btnCountdown.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (mIsPaused) {
                        btnCountdown.setText("Pause");
                        initTimer();
                    } else {
                        btnCountdown.setText("Play");
                        cancelTimer();
                    }
    
                    mIsPaused = !mIsPaused;
                }
            });
        }
    
        private void cancelTimer() {
    
            if (mTimer != null) {
                mTimer.cancel();
                mTimer = null;
            }
    
        }
    
        private void initTimer() {
            mTimer = new CountDownTimer(mMillisUntilFinish, 1000) {
                public void onTick(long millisUntilFinished) {
                    tvTimeUntilFinish.setText("seconds remaining: " + millisUntilFinished / 1000);
                    mMillisUntilFinish = millisUntilFinished;
                }
    
                public void onFinish() {
                }
            }.start();
    
        }
    }