-
public abstract class WakefulBroadcastReceiver extends BroadcastReceiver
Helper for the common pattern of implementing a BroadcastReceiver that receives a device wakeup event and then passes the work off to a android.app.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 WAKE_LOCK permission to use it.
ExampleA 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.
{@sample development/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulReceiver.java complete}
The service (in this example, an android.app.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.
{@sample development/samples/Support4Demos/src/com/example/android/supportv4/content/SimpleWakefulService.java complete}
-
-
Method Summary
Modifier and Type Method Description static ComponentNamestartWakefulService(Context context, Intent intent)Do a Context.startService, but holding a wake lock while the service starts. static booleancompleteWakefulIntent(Intent intent)Finish the execution from a previous startWakefulService. -
Methods inherited from class android.content.BroadcastReceiver
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getResultCode, getResultData, getResultExtras, goAsync, isInitialStickyBroadcast, isOrderedBroadcast, onReceive, peekService, setDebugUnregister, setOrderedHint, setResult, setResultCode, setResultData, setResultExtras -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
startWakefulService
static ComponentName startWakefulService(Context context, Intent intent)
Do a 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 in order to releasethe wake lock.
- Parameters:
context- The Context in which it operate.intent- The Intent with which to start the service, as per Context.startService.
-
completeWakefulIntent
static boolean completeWakefulIntent(Intent intent)
Finish the execution from a previous startWakefulService. Any wake lockthat was being held will now be released.
- Parameters:
intent- The Intent as originally generated by startWakefulService.
-
-
-
-