广播接收器、服务和唤醒锁
我在广播接收器中收到意图,然后我开始服务以完成更多工作。现在,如果设备处于睡眠状态并且发生这种情况怎么办,我是否必须获得唤醒锁(AlarmManger?),为什么我需要它? 如果设备在没有获得唤醒锁的情况下进入睡眠状态,我的服务是否会停止运行。
im receiving an intent in broadcast receiver and then i start service to do more work. now what if the device is sleep and this happen, do i have to get Wakelock (AlarmManger?), and why do i need it?
does my service will stop running if the device goes to sleep without getting a wakelock.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果设备一开始就处于睡眠状态,您将不会“在广播接收器中接收意图”,因为设备处于睡眠状态。
您并不“需要它”,除非您想确保设备在完成某些工作时保持运行。
是的。
If the device is asleep to begin with, you will not be "receiving an intent in broadcast receiver", because the device is asleep.
You don't "need it", unless you want to ensure the device stays running while you complete some work.
Yes.
看起来 Android 原生的 WakefulBroadcastReceiver 将是一个完美的解决方案为你。需要扩展它而不是常规的 BroadcastReceiver 并以“唤醒”方式在 onReceive() 中启动服务:
并表明您的工作已在服务的 >onHandleIntent(),调用
Looks like the Android's native WakefulBroadcastReceiver would be a perfect solution for you. Need to extend this rather than the regular BroadcastReceiver and start the service in the onReceive() in the "wakeful" manner:
and signal your work is done in the service's onHandleIntent(), calling
查看上面的代码..将其放入单独的类文件中,并在清单中为某些自定义意图定义它....现在该类将响应自定义意图...只需广播该意图即可由于唤醒锁是静态的,因此整个应用程序中的唤醒锁打开或关闭。就像这样:
上面的内容将在您的 Alarmmanager 代码中定义,以便安排调用
look at the above code ..put it in a separate class file and and in your manifest define it for some custom intent .... now that this class will respond to a custom intent ...just broadcast that intent and you can turn the wakelock on or off in your entire app since the wakelock is static..like this :
the above would be defined in your alarmmanager code so it schedules a call