Android - 使用 Timer 和 TimerTask 控制任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2161750/
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
Android - Controlling a task with Timer and TimerTask?
提问by Donal Rafferty
I am currently trying to set up a WiFi Scan in my Android application that scans for WiFi access points every 30 seconds.
我目前正在尝试在我的 Android 应用程序中设置 WiFi 扫描,每 30 秒扫描一次 WiFi 接入点。
I have used Timer and TimerTask to get the scan running correctly at the intervals which I require.
我已经使用 Timer 和 TimerTask 以我需要的时间间隔正确运行扫描。
However I want to be able to stop and start the scanning when the user presses a button and I am currently having trouble stopping and then restarting the Timer and TimerTask.
但是,我希望能够在用户按下按钮时停止和开始扫描,并且我目前无法停止然后重新启动 Timer 和 TimerTask。
Here is my code
这是我的代码
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
public void doWifiScan(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
wifiManager.scan(context);
Log.d("TIMER", "Timer set off");
}
});
}};
t.schedule(scanTask, 300, 30000);
}
public void stopScan(){
if(scanTask!=null){
Log.d("TIMER", "timer canceled");
scanTask.cancel();
}
}
So the Timer and Task start fine and the scan happens every 30 seconds however I cant get it to stop, I can stop the Timer but the task still occurs and scanTask.cancel() doesn't seem to work either.
所以计时器和任务开始正常,扫描每 30 秒发生一次,但我无法停止,我可以停止计时器但任务仍然发生,并且 scanTask.cancel() 似乎也不起作用。
Is there a better way to do this? Or am I missing something in the Timer/TimerTask classes?
有一个更好的方法吗?或者我在 Timer/TimerTask 类中遗漏了什么?
采纳答案by CommonsWare
You might consider:
你可能会考虑:
- Examining the boolean result from calling
cancel()
on your task, as it should indicate if your request succeeds or fails - Try
purge()
orcancel()
on theTimer
instead of theTimerTask
- 检查调用
cancel()
您的任务的布尔结果,因为它应该表明您的请求是成功还是失败 - 尝试
purge()
或cancel()
在Timer
而不是TimerTask
If you do not necessarily need Timer
and TimerTask
, you can always use postDelayed()
(available on Handler and on any View
). This will schedule a Runnable
to be executed on the UI thread after a delay. To have it recur, simply have it schedule itself again after doing your periodic bit of work. You can then monitor a boolean flag to indicate when this process should end. For example:
如果您不一定需要Timer
and TimerTask
,您可以随时使用postDelayed()
(在 Handler 和 any 上可用View
)。这将安排Runnable
在延迟后在 UI 线程上执行。要让它再次发生,只需在完成您的定期工作后重新安排它自己。然后,您可以监视布尔标志以指示此过程何时结束。例如:
private Runnable onEverySecond=new Runnable() {
public void run() {
// do real work here
if (!isPaused) {
someLikelyWidget.postDelayed(onEverySecond, 1000);
}
}
};
回答by tony gil
using your code, instead of
使用您的代码,而不是
scanTask.cancel();
the correct way is to cancel your timer (not timerTask):
正确的方法是取消你的计时器(不是 timerTask):
t.cancel();
回答by Mike
The Android documentation says that cancel() Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing. Which explains the issue.
Android 文档说 cancel() 取消计时器和所有计划任务。如果有当前正在运行的任务,它不会受到影响。不能在此计时器上安排更多任务。后续调用什么都不做。这解释了这个问题。