由于 Timer() 的准确性,我使用它,但其工作方式与 PostDelayed Handler 相同。它只被调用一次。这是计时器代码:
public void setWFT() {
WFT = new Timer();
WFT.schedule(new TimerTask() {
@Override
public void run() {
WFTTimerMethod();
}
}, 60000); // 60 seconds delay
}
private void WFTTimerMethod() {
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
// My commands here
}
};
一旦定时器启动,这只会在 60 秒后调用 run() 一次。有时,我必须取消计时器来更新延迟(替换“60000”值)。要再次启动计时器,我只需使用新的延迟值再次调用 WFT() 来重新创建计时器。
问题是,当我使用以下方法取消计时器时:
WFT.cancel();
WFT.purge();
计时器未启动。 run() 没有在应该执行的时候执行。所以我的问题是我应该使用cancel()和purge()还是只使用cancel()?
谢谢
最佳答案
From the Java API在 purge()
上:
Most programs will have no need to call this method. It is designed for use by the rare application that cancels a large number of tasks. Calling this method trades time for space: the runtime of the method may be proportional to n + c log n, where n is the number of tasks in the queue and c is the number of cancelled tasks.
所以你只需要调用cancel()
关于java - Android Java 定时器(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061874/