如何在Android中停止CountDownTimer [英] How to stop CountDownTimer in android
本文介绍了如何在Android中停止CountDownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何停止此计时器,知道吗?
How to stop this timer , any idea ?
我想在每个查询中重置计时器,但它会继续.每个新查询都会添加新计时器.该如何解决?
I want to reset timer in every query but it continues. Every new query it adds new timer. How to solve this?
new CountDownTimer(zaman, 1000) { //geriye sayma
public void onTick(long millisUntilFinished) {
NumberFormat f = new DecimalFormat("00");
long hour = (millisUntilFinished / 3600000) % 24;
long min = (millisUntilFinished / 60000) % 60;
long sec = (millisUntilFinished / 1000) % 60;
cMeter.setText(f.format(hour) + ":" + f.format(min) + ":" + f.format(sec));
}
public void onFinish() {
cMeter.setText("00:00:00");
}
}.start();
推荐答案
您可以将其分配给变量,然后在变量上调用cancel()
You can assign it to a variable and then call cancel()
on the variable
CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {}
}.start();
yourCountDownTimer.cancel();
或者您可以在计数器范围内调用cancel()
or you can call cancel()
inside of your counter scope
new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {
cancel();
}
public void onFinish() {}
}.start();
了解更多: https://developer.android.com/reference/android/os/CountDownTimer.html
这篇关于如何在Android中停止CountDownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文