18

我必须重新启动 CountDownTimer。我在这里阅读了很多问题,但没有一个答案对我有帮助。当我使用以下代码时

if(Const.counter != null){
    Const.counter.cancel();
    Const.counter = null;
}


Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000);
Const.counter.start();

我开了一个新柜台,但旧柜台也继续工作。请帮我解决它。

4

5 回答 5

14

您可以通过取消并重新启动来实现它。以下示例应该可以工作。

CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {}

    @Override
    public void onFinish() {
        isCounterRunning = false;
    }
};


boolean isCounterRunning  = false;

private void yourOperation() {
    if( !isCounterRunning ){
        isCounterRunning = true;
        mCountDownTimer.start();
    }
    else{
        mCountDownTimer.cancel(); // cancel
        mCountDownTimer.start();  // then restart
    }

}
于 2014-05-22T01:49:43.127 回答
6

我在这里做了一些不同的把戏。希望这会帮助你。

if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
        myCountDownTimer = new MyCountDownTimer(10000, 500);
        myCountDownTimer.start();
于 2017-09-14T18:55:47.753 回答
1

测验的倒计时

 if(countDownTimer!=null)
            {
                countDownTimer.cancel();
                countDownTimer.start();
                }
            else {
               countDownTimer = new CountDownTimer(30000, 1000) {

                    public void onTick(long l) {
                        mtimer.setText("remaining time" + l / 1000);//mtime is a textview
                    }

                    public void onFinish() {//here mnext is the button from which we can get next question.
                        mnext.performClick();//this is used to perform clik automatically

                    }
                }.start();
于 2019-07-29T17:27:54.573 回答
1
private fun startTimer() {
    var timeInMilliSeconds = 11000L
    val countDownTimer: CountDownTimer = object : CountDownTimer(timeInMilliSeconds, 1000) {
        override fun onFinish() {
            Timber.d("Times Up!")
            setupResult("")
            this.cancel()
            timeInMilliSeconds = 11000L
            this.start()
        }

        override fun onTick(p0: Long) {
            val seconds = (p0 / 1000) % 60
            Timber.d("Timer: $p0")
            timer?.text = "$seconds"
        }
    }
    countDownTimer.start()
}
于 2020-07-10T19:25:10.440 回答
1

只需再次调用该start()方法:

CountDownTimer cdt = new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        this.start(); //start again the CountDownTimer
    }
};
于 2019-12-28T22:33:11.910 回答