TimerTask Android 蓝牙
我正在实现一个在 Android 启动时启动的服务,它应该每 10 分钟扫描一次附近的蓝牙设备。当它发现设备时,它会执行一些工作。此外,这种定期扫描应该在设备开启的整个过程中进行。我正在尝试安排一个 TimerTask,但我不明白如何使用它。我想它应该启动这个服务并让该服务完成工作而不是在TimerTask的run方法中编写代码?我如何从 TimerTask 启动服务,因为这似乎是解决我的问题的最简单方法,但 TimerTask 是 java.util 的一部分,而不是 Android 的类之一。
我刚刚找到了Android的AlarmManager。我应该用那个吗?它可以启动一个服务吗?
到目前为止我已经有了这个,但我需要帮助:
class Timer extends Service { private Handler myHander;Runnable r = new Runnable() { run() { startService(new Intent(Timer.this.getApplicationContext() ,MyService.class));
myHandler.postDelayed(r,10 分钟); } }
onCreate() { myHandler=new MyHandler(); } onStartCommand() { //Do the bluetooth work.
r.run(); }
onDestroy() { super.onDestroy(); myHandler.removeCallback(r); }
} MyService 类扩展了 Service { }
抱歉,我不明白这里的格式是如何工作的。
我需要重写服务中的 onDestroy() 吗?在哪里使用 stopService() ?
I am implementing a Service that starts when Android boots, and it's supposed to scan every 10 mins for nearby Bluetooth devices. When it discovers devices, it does some work. Also, this periodic scanning should occur the entire time the device is on. I am trying to schedule a TimerTask, but I don't understand how to use it. I guess it should start this service and let the service do the work instead of writing the code in the TimerTask's run method? How would I start the service from the TimerTask as this seems the easiest way to remedy my problem, but TimerTask is part of java.util and not one of Android's classes.
I just found Android's AlarmManager. Should I use that? Can it start a Service?
So far I have this, but I need help:
class Timer extends Service { private Handler myHander;
Runnable r = new Runnable()
{
run()
{
startService(new Intent(Timer.this.getApplicationContext() ,MyService.class));
myHandler.postDelayed(r,10 minutes);
}
}
onCreate()
{
myHandler=new MyHandler();
}
onStartCommand()
{
//Do the bluetooth work.
r.run();
}
onDestroy()
{
super.onDestroy();
myHandler.removeCallback(r);
}
}
class MyService extends Service
{
}
Sorry, I don't understand how the formatting works here.
Will I need to override onDestroy() in the Service? Where to do I use stopService() ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要:
onStart
/onStartCommand
中的服务中,您需要使用Handler
或AlaramManager
安排定期更新。在这种情况下,
Handler
和AlarmManager
之间的区别是:当设备处于深度睡眠时,Handler
将不会运行,而您可以配置< code>AlaramManager 唤醒设备并运行您的代码。我建议使用
Handler
,因为它更容易,而且因为您说您只需要在设备唤醒时运行代码。还有一件事,
Handler
不使用额外的线程,而TimerTask
则使用额外的线程。在 Android 上,这被认为是一种不好的做法,仅浪费计时器的线程。有关如何使用
Handler
重复任务的示例代码可以在此处找到:延迟重复任务?。You need to:
onStart
/onStartCommand
you need to schedule either usingHandler
orAlaramManager
periodic updates.The difference between
Handler
andAlarmManager
in this case will be that:Handler
will not run when device is in deep sleep, while you can configureAlaramManager
to wake up the device and run your code.I'd recommend to go with
Handler
, as its easier and because you said you only need to run your code when device is awake.And one more thing,
Handler
doesn't use extra thread whileTimerTask
does. And this is considered a bad practice on Android to waste on thread for timer only.An example code for how to repeat task using
Handler
can be found here: Repeat a task with a time delay?.