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



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

"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."
  • http://developer.android.com/reference/android/app/AlarmManager.html

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

谢谢

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

最新更新