如果报警与服务或广播接收器建? [英] Should an alarm be built with Service or BroadcastReceiver?
问题描述
我想建立一个报警程序。我见过一些例子,其中有些使用服务和一些使用BroadcasterReceiver。用户将设置报警,然后当它熄灭他们将不得不做某些事情像解决一个数学方程或扫描NFC标签它关闭之前。我应该使用哪一种?
I want to build an alarm application. I've seen some examples and some of them use Service and some use BroadcasterReceiver. The user will set the alarm and then when it goes off they'll have to do certain things like solve a mathematical equation or scan NFC tag before it turns off. Which one should I use?
推荐答案
如果您使用的是 AlarmManager
与 _WAKEUP
报警,你的必须的有一个广播接收器
的的PendingIntent
路线。唯一的Android保障与 _WAKEUP
报警是,如果你使用广播接收器
,Android将保持设备清醒足够长的时间为的onReceive()
来完成。别的,全盘皆输。
If you are using AlarmManager
with a _WAKEUP
alarm, you must have the PendingIntent
route to a BroadcastReceiver
. The only thing Android guarantees with a _WAKEUP
alarm is if you use a BroadcastReceiver
, Android will keep the device awake long enough for onReceive()
to complete. Anything else, all bets are off.
这你想要做将需要超过几毫秒的更多的工作,有广播接收器
回身控制传递给一个服务,它可以在做其工作后台线程。你不妨使用我的 WakefulIntentService
以那;如果没有,你需要管理自己的激活锁定
来确保设备保持清醒,直到服务可以完成工作。
It the work you want to do would take more than a couple of milliseconds, have the BroadcastReceiver
turn around and pass control to a service, which can do its work on a background thread. You may wish to use my WakefulIntentService
for that; if not, you will need to manage your own WakeLock
to ensure that the device stays awake until the service can complete its work.
这篇关于如果报警与服务或广播接收器建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!