如何停止/取消android CountDownTimer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3138348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:52:27  来源:igfitidea点击:

how to stop/cancel android CountDownTimer

android

提问by rantravee

I'm extending the CountDownTimer class to obtain some custom functionality .In onTick() in case some conditions are met I call cancel() , expecting that will be the end of it, however the onTick() callback gets call until the the count down is reached . So how to prevent this from happening ?

我正在扩展 CountDownTimer 类以获得一些自定义功能。在 onTick() 中,如果满足某些条件,我会调用 cancel() ,预计这将是它的结束,但是 onTick() 回调会调用直到计数下来是达到了。那么如何防止这种情况发生呢?

回答by Fedor

CountDownTimer.cancel() method seems to be not working. Here's another thread without a solution Timer does not stop in android.

CountDownTimer.cancel() 方法似乎不起作用。这是另一个没有解决方案的线程Timer 在 android 中不会停止

I would recommend you to use Timer instead. It's much more flexible and can be cancelled at any time. It may be something like that:

我建议您改用 Timer 。它更加灵活,可以随时取消。它可能是这样的:

public class MainActivity extends Activity {    
    TextView mTextField;
    long elapsed;
    final static long INTERVAL=1000;
    final static long TIMEOUT=5000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextField=(TextView)findViewById(R.id.textview1);

        TimerTask task=new TimerTask(){
            @Override
            public void run() {
                elapsed+=INTERVAL;
                if(elapsed>=TIMEOUT){
                    this.cancel();
                    displayText("finished");
                    return;
                }
                //if(some other conditions)
                //   this.cancel();
                displayText("seconds elapsed: " + elapsed / 1000);
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
    }

    private void displayText(final String text){
        this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                mTextField.setText(text);
            }});
    }
}

回答by maid450

CountDownTimer is also working fine for me, but I think it only works if you call it OUTSIDE of the CountDownTimer implemetation (that is don't call it in the onTick).

CountDownTimer 对我来说也很好用,但我认为只有在 CountDownTimer 实现之外调用它才有效(即不要在 onTick 中调用它)。

Calling it inside also didn't worked.

在里面调用它也没有用。

回答by Joseph Mhanna

I tried this code snippet, since most answers are saying you cannot cancel the timer inside its implementation, thus i tried using a handler inside onFinish. Old post but if anyone comes across this its helpful.

我尝试了这个代码片段,因为大多数答案都说你不能在它的实现中取消计时器,因此我尝试在 onFinish 中使用一个处理程序。旧帖子,但如果有人遇到它,它会有所帮助。

new Handler().post(new Runnable() {
            @Override
            public void run() {
                timerTextView.setText("00:" + String.format("%02d", counter));
                cancel();
            }
        });