停止并重新启动CountDownTimer

时间:2016-04-12 16:16:13

标签: android

我有一个Timer它从00:00开始很好,但是当我想要停止它时我会调用cdTimer.cancel()但是我无法重新启动Timer旧的价值观又重新开始。我有这个方法:

private void StartTimer(){
    final long EndTime   = 3600;
    cdtTimer = new CountDownTimer(EndTime*1000, 1000) {
        public void onTick(long millisUntilFinished) {
            long secondUntilFinished = (long) (millisUntilFinished/1000);
            long secondsPassed = (EndTime - secondUntilFinished);
            long minutesPassed = (long) (secondsPassed/60);
            secondsPassed = secondsPassed%60;
            tvCounterTimer.setText(String.format("%02d", minutesPassed) + ":" + String.format("%02d", secondsPassed));
        }
        public void onFinish() {
            tvCounterTimer.setText("done!");
        }
    }.start();
}

我以为我可以保存值minutesPassedsecondsPassed,但我应该通过参数传递它们,对吗?

1 个答案:

答案 0 :(得分:0)

要使计时器执行此操作

timer.cancel();
timer.purge();

要重新启动计时器,您必须重新实例化该对象,然后安排它。我会重新开始。