问题
Should alarm manager send PendingIntent to IntentService or is it better to send it to a broadcast receiver which then issues startService() targeting the pending intent?
回答1:
That depends a bit on whether this is a _WAKEUP
alarm or not.
If not, feel free to use a getService()
PendingIntent
.
If, however, it is a _WAKEUP
alarm, not only do you need to use a BroadcastReceiver
intermediary, but that BroadcastReceiver
must also acquire a WakeLock
which the IntentService
later releases. The BroadcastReceiver
requirement is due to the way AlarmManager
handles its own WakeLock
. I have wrapped all of this up as a WakefulIntentService to simplify the implementation.
来源:https://stackoverflow.com/questions/4982920/intentservice-and-alarmmanager-communication