![](/img/trans.png)
[英]How to countDownTimer.cancel(); in OnTick(); function
[英]CountDownTimer.cancel() is not working in Android
CountDownTimer.cancel()
在以下代码中不起作用:
myTimer = new CountDownTimer(10000, 1000) {
public void onFinish() {
}
@Override
public void onTick(long millisUntilFinished) {
if(null != myObject){
myTimer.cancel();
}
}
}.start();
在上面的代码中,我启动了一个CountDownTimer
,它检查对象是否为null
并相应地取消Timer。 该对象由某个侦听器在任何时间点设置。 请参考并提出建议。 我在这做对了吗?
解决方案作者:Gautier Hayoun :
刚刚为CountDownTimer做了替代,你可以在onTick中取消: Github链接 - Gautier Hayoun 2010年12月12日在1:04
解决方案作者:Gautier Hayoun:
刚刚为CountDownTimer做了替代,你可以在onTick中取消: Github链接 - Gautier Hayoun 2010年12月12日在1:04
而不是CountDownTimer使用TimerTask
final static long INTERVAL=1000;
final static long TIMEOUT=10000;
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);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.