Wake Service directly from AlarmManager
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 su开发者_StackOverflowre 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?
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.
精彩评论