android.support.v4.content
Class WakefulBroadcastReceiver

java.lang.Object
  extended by android.content.BroadcastReceiver
      extended by android.support.v4.content.WakefulBroadcastReceiver

public abstract class WakefulBroadcastReceiver
extends android.content.BroadcastReceiver

Helper for the common pattern of implementing a 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 Manifest.permission.WAKE_LOCK permission to use it.

Example

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.


Constructor Summary
WakefulBroadcastReceiver()
           
 
Method Summary
static boolean completeWakefulIntent(android.content.Intent intent)
          Finish the execution from a previous startWakefulService(android.content.Context, android.content.Intent).
static android.content.ComponentName startWakefulService(android.content.Context context, android.content.Intent intent)
          Do a Context.startService, but holding a wake lock while the service starts.
 
Methods inherited from class android.content.BroadcastReceiver
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getResultCode, getResultData, getResultExtras, 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
 

Constructor Detail

WakefulBroadcastReceiver

public WakefulBroadcastReceiver()
Method Detail

startWakefulService

public static android.content.ComponentName startWakefulService(android.content.Context context,
                                                                android.content.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(android.content.Intent) in order to release the wake lock.

Parameters:
context - The Context in which it operate.
intent - The Intent with which to start the service, as per Context.startService.

completeWakefulIntent

public static boolean completeWakefulIntent(android.content.Intent intent)
Finish the execution from a previous startWakefulService(android.content.Context, android.content.Intent). Any wake lock that was being held will now be released.

Parameters:
intent - The Intent as originally generated by startWakefulService(android.content.Context, android.content.Intent).
Returns:
Returns true if the intent is associated with a wake lock that is now released; returns false if there was no wake lock specified for it.