Wake Service directly from AlarmManager

谁说我不能喝 提交于 2019-11-29 19:14:25

问题


In the https://github.com/commonsguy/cwac-wakeful demo, the OnAlarmReceiver (a BroadcastReceiver) onReceive() method is called in response to an Alarm. The onReceive() method starts the Service. There are two Intents used, one received by the BroadcastReceiver and one by the Service. This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

I understand that the Phone is guaranteed not to sleep while onReceive() is executing i.e. it wrapped with a wake lock. I'm not sure if the Service class offers any similar guarantees.

Is the any way to start the Service directly from an Alarm while still guaranteeing that the Phone will be woken from sleep and won't sleep until a wake lock can be acquired?


回答1:


This seems more complicated that it needs to be, why not just have the Service receive the Intent from the AlamManager?

Because Android will not guarantee that the device will stay awake long enough for a getService() PendingIntent to be invoked. The guarantee is only for a getBroadcast() PendingIntent -- Android ensures that the device will stay awake (via a WakeLock) for the duration of your onReceive() call.

Trust me, I wish we could skip it.



来源:https://stackoverflow.com/questions/7444139/wake-service-directly-from-alarmmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!