如何在 android 中停止 CountDownTimer
How to stop CountDownTimer in android
如何停止这个计时器,知道吗?
我想在每个查询中重置计时器,但它仍在继续。每个新查询都会添加新的计时器。如何解决?
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()
CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {}
}.start();
yourCountDownTimer.cancel();
或者您可以在计数器范围内调用 cancel()
new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {
cancel();
}
public void onFinish() {}
}.start();
阅读更多:https://developer.android.com/reference/android/os/CountDownTimer.html
您可以在其回调方法之一中调用 this.cancel()
或简单地调用 cancel()
static CountDownTimer countDownTimer = null;
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished) {
Log.e("seconds remaining : ", "seconds remaining : " + millisUntilFinished / 1000);
}
public void onFinish() {
Log.e("done!", "done!");
}
};
countDownTimer.start();
在 kotlin 中你可以使用这个命令
countDownTimer?.cancel()
如何停止这个计时器,知道吗?
我想在每个查询中重置计时器,但它仍在继续。每个新查询都会添加新的计时器。如何解决?
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()
CountDownTimer yourCountDownTimer = new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {}
public void onFinish() {}
}.start();
yourCountDownTimer.cancel();
或者您可以在计数器范围内调用 cancel()
new CountDownTimer(zaman, 1000) {
public void onTick(long millisUntilFinished) {
cancel();
}
public void onFinish() {}
}.start();
阅读更多:https://developer.android.com/reference/android/os/CountDownTimer.html
您可以在其回调方法之一中调用 this.cancel()
或简单地调用 cancel()
static CountDownTimer countDownTimer = null;
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(50000, 1000) {
public void onTick(long millisUntilFinished) {
Log.e("seconds remaining : ", "seconds remaining : " + millisUntilFinished / 1000);
}
public void onFinish() {
Log.e("done!", "done!");
}
};
countDownTimer.start();
在 kotlin 中你可以使用这个命令
countDownTimer?.cancel()