Class WakefulBroadcastReceiver

  • Direct Known Subclasses:
    BatchPushMessageDismissReceiver, BatchPushMessageReceiver, BatchPushReceiver

    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.

    • Nested Class Summary

      • Nested classes/interfaces inherited from class android.content.BroadcastReceiver

        android.content.BroadcastReceiver.PendingResult
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean completeWakefulIntent​(android.content.Intent 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, 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
    • 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.