java 当我调用取消时 timer.scheduleAtFixedRate 不会停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14252592/
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
timer.scheduleAtFixedRate does not stop when i call cancel
提问by Daler
At onCreate, I run a task that repeats every minute. Here how I do it:
在 onCreate 中,我运行每分钟重复一次的任务。这里我是怎么做的:
myTimer = new Timer();
int delay = 30000; // delay for 30 sec.
int period = 600000; // repeat every 60 sec.
doThis = new TimerTask() {
public void run() {
Log.v("TImer","repeated");
wv.reload();
}
};
myTimer.scheduleAtFixedRate(doThis, delay, period);
All that code is in onCreate. So, when the app goes off from the screen, I can see in logcat that timer steel runs, and will not stop unless the app will be destroyed. So, in onPause
of activity I call myTimer.cancel();
but it didnt help. I still can see updates in logcat, even when the app is not on the screen. So, how to stop timerTask
?
所有这些代码都在 onCreate 中。因此,当应用程序从屏幕上消失时,我可以在 logcat 中看到计时器钢正在运行,并且不会停止,除非应用程序被销毁。所以,在onPause
活动中我打电话myTimer.cancel();
但没有帮助。即使应用程序不在屏幕上,我仍然可以在 logcat 中看到更新。那么,如何停止timerTask
?
采纳答案by Anthony Tietjen
Here is your code put into my file, with the values delay and period tweaked so I don't have to wait so long. I run the app. I see the messages in LogCat. I press the home button on my Galaxy S3. Then the messages stop in LogCat.
这是您放入我的文件中的代码,延迟和周期值已调整,因此我不必等待太久。我运行应用程序。我在 LogCat 中看到了消息。我按下了 Galaxy S3 上的主页按钮。然后消息在 LogCat 中停止。
public class MainActivity extends Activity {
Timer myTimer;
TimerTask doThis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTimer = new Timer();
int delay = 0; // delay for 30 sec.
int period = 1000; // repeat every 60 sec.
doThis = new TimerTask() {
public void run() {
Log.v("TImer","repeated");
}
};
myTimer.scheduleAtFixedRate(doThis, delay, period);
}
@Override
protected void onPause() {
myTimer.cancel();
super.onPause();
}
}
回答by jox
The actual answer is: The onPause
method needs to be defined correctly.
实际答案是:onPause
需要正确定义方法。
The whole story:
The questioner defined the onPause
method wrong. That was the reason for asking this question.
整个故事:
提问者定义了onPause
错误的方法。这就是问这个问题的原因。
I'm writing this because I spent too much time reading question, answers, code and comments. The real answer was hidden in the last comment.
我写这篇文章是因为我花了太多时间阅读问题、答案、代码和评论。真正的答案隐藏在最后一条评论中。
回答by Anthony Tietjen
It could be that since the thread has already been sent out, it runs one last time. In the onLoad set a variable to true, then in the onPause set it to false. Then in your timer task only run your code if the variable is true.
可能是因为线程已经发出,它最后运行一次。在 onLoad 中将一个变量设置为 true,然后在 onPause 中将其设置为 false。然后在您的计时器任务中,如果变量为真,则仅运行您的代码。
Write to the log outside of the new if statement though. If it is indeed running it just one last time, then that might be your solution. But if it is still running it over and over multiple times after the onPause then don't take my solution.
但是,写入新 if 语句之外的日志。如果它确实只是最后一次运行它,那么这可能是您的解决方案。但是如果在 onPause 之后它仍然反复运行它,那么不要采用我的解决方案。