CountDownTimer in android - how to restart it

橙三吉。 提交于 2019-12-04 00:30:29

You can realize it by cancelling and restarting. The following example should work.

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
    }

}

I did some different trick here. Hope this will help you.

if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
        myCountDownTimer = new MyCountDownTimer(10000, 500);
        myCountDownTimer.start();

coutdown timer for quiz

 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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!