警报管理器,使用唤醒锁来确保所有代码运行

发布于 2024-11-06 11:57:17 字数 712 浏览 2 评论 0原文

我正在考虑使用警报管理器,并在开发人员文档中阅读了此内容,但我不太明白。

"If your alarm receiver called Context.startService(),
it is possible that the phone will sleep before the
requested service is launched. To prevent this, your
BroadcastReceiver and Service will need to implement a
separate wake lock policy to ensure that the phone
continues running until the service becomes available."

我特别想问在哪些情况下手机可能会在服务启动之前进入睡眠状态(因为这是我不理解的部分)?它取决于手机执行语句的速度吗? IE。它调用 startService() 来打开另一个线程,因此原始线程可以在服务可用之前完成其工作?

谢谢

I'm looking at using the Alarm Manager, and read this in the developer docs, which I don't really understand.

"If your alarm receiver called Context.startService(),
it is possible that the phone will sleep before the
requested service is launched. To prevent this, your
BroadcastReceiver and Service will need to implement a
separate wake lock policy to ensure that the phone
continues running until the service becomes available."

I am specifically asking for which situations it could possible that the phone will sleep before the service is launched (as this is the part I don't comprehend)? Is it dependent on how fast the phone can execute statements? ie. it calls startService() which opens another thread and so the original thread could complete its work before the service has been made available?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-11-13 11:57:17

如果您从 BroadcastReceiver 启动服务,则只能保证设备在接收器的 onReceive() 期间不会休眠。根据这个问题,startService()是异步的,这意味着它不会阻止onReceive()完成当服务正在启动时。所以如果你需要确保服务启动,你必须实现自己的WakeLock。

If you're starting the service from a BroadcastReceiver, you're only guaranteed that the device will not sleep during the receiver's onReceive(). According to this question, startService() is asynchronous, which means it will not block onReceive() from finishing while the service is being started. So if you need to make sure that the service starts, you have to implement your own WakeLock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文