public abstract class WakefulBroadcastReceiver extends BroadcastReceiver
BroadcastReceiver
that receives a device wakeup event and then passes the work off
to a Service
, while ensuring that the
device does not go back to sleep during the transition.
This class takes care of creating and managing a partial wake lock
for you; you must request the android.Manifest.permission#WAKE_LOCK
permission to use it.
A WakefulBroadcastReceiver
uses the method
startWakefulService()
to start the service that does the work. This method is comparable to
startService()
, except that
the WakefulBroadcastReceiver
is holding a wake lock when the service
starts. The intent that is passed with
startWakefulService()
holds an extra identifying the wake lock.
The service (in this example, an IntentService
) does
some work. When it is finished, it releases the wake lock by calling
completeWakefulIntent(intent)
. The intent it passes as a parameter
is the same intent that the WakefulBroadcastReceiver
originally
passed in.
BroadcastReceiver.PendingResult
Constructor and Description |
---|
WakefulBroadcastReceiver() |
Modifier and Type | Method and Description |
---|---|
static boolean |
completeWakefulIntent(Intent intent)
Finish the execution from a previous
startWakefulService(android.content.Context, android.content.Intent) . |
static ComponentName |
startWakefulService(Context context,
Intent intent)
Do a
Context.startService , but holding a wake lock while the service starts. |
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getPendingResult, getResultCode, getResultData, getResultExtras, getSendingUserId, goAsync, isInitialStickyBroadcast, isOrderedBroadcast, onReceive, peekService, setDebugUnregister, setOrderedHint, setPendingResult, setResult, setResultCode, setResultData, setResultExtras
public static ComponentName startWakefulService(Context context, Intent intent)
Context.startService
, but holding a wake lock while the service starts.
This will modify the Intent to hold an extra identifying the wake lock;
when the service receives it in Service.onStartCommand
, it should pass back the Intent it receives there to
completeWakefulIntent(android.content.Intent)
in order to release
the wake lock.context
- The Context in which it operate.intent
- The Intent with which to start the service, as per
Context.startService
.public static boolean completeWakefulIntent(Intent intent)
startWakefulService(android.content.Context, android.content.Intent)
. Any wake lock
that was being held will now be released.intent
- The Intent as originally generated by startWakefulService(android.content.Context, android.content.Intent)
.