广播接收器android的计时器
我正在开发这个应用程序,我要扫描可到达的接入点。我必须每隔一秒就定期这样做。 我开始用普通的timerTask来做这件事,但效果不佳,因为它总是创建新线程。因此,我开始在 android 中使用处理程序类并调用 postDelayed 方法来安排扫描!就像这样:
protected void setTimer()
{
final long elapse = 100;
Runnable t = new Runnable() {
public void run()
{
Log.i(TAG3, "startedScan");
IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(wifiReceiver, filter);
wifiManager.startScan();
if( !isComplete )
{
mHandler.postDelayed( this, elapse );
}
}
};
mHandler.postDelayed( t, elapse );
}
问题是扫描只运行 3 次,然后就再也不会运行了。我找不到解决方案!我该如何解决这个问题?
I'm developing this application were I do a scan for the reachable access points. I have to do it periodicaly only second after second.
I started to do it with a ordinary timerTask, but it didn't worked well because it is alaways creating new threads. So, I started using the handler class in android and calling a postDelayed method to schedule the scan!just like this:
protected void setTimer()
{
final long elapse = 100;
Runnable t = new Runnable() {
public void run()
{
Log.i(TAG3, "startedScan");
IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(wifiReceiver, filter);
wifiManager.startScan();
if( !isComplete )
{
mHandler.postDelayed( this, elapse );
}
}
};
mHandler.postDelayed( t, elapse );
}
The problem is that the scan is only running 3 times and then it never runns again..I can't find a solution!How can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测 isComplete 被设置为 true,因此 Runnable 不会被重新安排。我建议将 Runnable 移出该方法,然后将重新安排添加到 wifiReceiver 的 onReceive 方法中。
I'm guessing
isComplete
is getting set to true, so the Runnable isn't being re-scheduled. I'd suggest moving theRunnable
out of the method, and then adding the reschedule towifiReceiver
sonReceive
method.