暂停后继续倒计时

Continue countdown timer after pause

我有一个倒数计时器,我有一个暂停它的按钮,但我需要在您单击按钮时继续倒计时。我搜索但无法找到与此相关的功能。怎么办?这是我的代码,我只成功重启了它,但没有继续:

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);

          }
         }

我考虑过将 millisUntilFinished 设为变量,但没有成功。不管怎样,我想这条路很接近。

您可以尝试保存秒数直到完成,然后您可以使用该秒数启动新的倒数计时器。

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

当您按下暂停按钮时,它会保存秒数,直到计时器结束。因此,当您再次按下播放键时,您会创建一个新的 CountDownTimer,其中包含您错过的那些秒数。

更新

我举了个例子:

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();

    }
}