# WakefulBroadcastReceiver

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:support-core-utils:28.0.0-alpha1  
Deprecated since version 26.1.0  
Summary:[Ctors](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#)  

# WakefulBroadcastReceiver

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


abstract
class
WakefulBroadcastReceiver
`  
`


``

extends `[BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html)`


``


`

|---|---|-----------------------------------------------------|
| java.lang.Object                                          |||
| ↳ | [android.content.BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html) ||
|   | ↳ | android.support.v4.content.WakefulBroadcastReceiver |

<br />

*** ** * ** ***

<br />

**This class was deprecated in API level 26.1.0.**   
As of[Android O](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#O), background check restrictions make this class no longer generally useful. (It is generally not safe to start a service from the receipt of a broadcast, because you don't have any guarantees that your app is in the foreground at this point and thus allowed to do so.) Instead, developers should use android.app.job.JobScheduler to schedule a job, and this does not require that the app hold a wake lock while doing so (the system will take care of holding a wake lock for the job).

This helper is for an old pattern of implementing a[BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html)that receives a device wakeup event and then passes the work off to a[Service](https://developer.android.com/reference/android/app/Service.html), 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](https://developer.android.com/reference/android/Manifest.permission#WAKE_LOCK)permission to use it.

Wakelocks held by this class are reported to tools as`"androidx.core:wake:<component-name>"`.

### Example

A[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver)uses the method[startWakefulService()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent))to start the service that does the work. This method is comparable to[startService()](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)), except that the[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver)is holding a wake lock when the service starts. The intent that is passed with[startWakefulService()](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent))holds an extra identifying the wake lock.  

```
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class SimpleWakefulReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // This is the Intent to deliver to our service.
        Intent service = new Intent(context, SimpleWakefulService.class);

        // Start the service, keeping the device awake while it is launching.
        Log.i("SimpleWakefulReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(context, service);
    }
}
```

The service (in this example, an[IntentService](https://developer.android.com/reference/android/app/IntentService.html)) does some work. When it is finished, it releases the wake lock by calling[completeWakefulIntent(intent)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#completeWakefulIntent(android.content.Intent)). The intent it passes as a parameter is the same intent that the[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver)originally passed in.  

```
import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

public class SimpleWakefulService extends IntentService {
    public SimpleWakefulService() {
        super("SimpleWakefulService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // At this point SimpleWakefulReceiver is still holding a wake lock
        // for us.  We can do whatever we need to here and then tell it that
        // it can release the wakelock.  This sample just does some slow work,
        // but more complicated implementations could take their own wake
        // lock here before releasing the receiver's.
        //
        // Note that when using this approach you should be aware that if your
        // service gets killed and restarted while in the middle of such work
        // (so the Intent gets re-delivered to perform the work again), it will
        // at that point no longer be holding a wake lock since we are depending
        // on SimpleWakefulReceiver to that for us.  If this is a concern, you can
        // acquire a separate wake lock here.
        for (int i=0; i<5; i++) {
            Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
                    + "/5 @ " + SystemClock.elapsedRealtime());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
        }
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }
}
```

<br />

## Summary

|                                                                    ### Public constructors                                                                    ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#WakefulBroadcastReceiver())`() ` |

|                                                                                                                                                                                                                                                                                                                                ### Public methods                                                                                                                                                                                                                                                                                                                                ||
|-------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static boolean`                                                                                     | ` `[completeWakefulIntent](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#completeWakefulIntent(android.content.Intent))`(`[Intent](https://developer.android.com/reference/android/content/Intent.html)` intent) ` Finish the execution from a previous[startWakefulService(Context, Intent)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent)).                                                            |
| ` static `[ComponentName](https://developer.android.com/reference/android/content/ComponentName.html) | ` `[startWakefulService](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent))`(`[Context](https://developer.android.com/reference/android/content/Context.html)` context, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` intent) ` Do a[Context.startService](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)), but holding a wake lock while the service starts. |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#)From class` `[android.content.BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html)` ` |------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` final void`                                                                                                                            | ` abortBroadcast() `                                                                                                                                                                          | | ` final void`                                                                                                                            | ` clearAbortBroadcast() `                                                                                                                                                                     | | ` final boolean`                                                                                                                         | ` getAbortBroadcast() `                                                                                                                                                                       | | ` final boolean`                                                                                                                         | ` getDebugUnregister() `                                                                                                                                                                      | | ` final int`                                                                                                                             | ` getResultCode() `                                                                                                                                                                           | | ` final String`                                                                                                                          | ` getResultData() `                                                                                                                                                                           | | ` final `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)                                                        | ` getResultExtras(boolean arg0) `                                                                                                                                                             | | ` final `[BroadcastReceiver.PendingResult](https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html) | ` goAsync() `                                                                                                                                                                                 | | ` final boolean`                                                                                                                         | ` isInitialStickyBroadcast() `                                                                                                                                                                | | ` final boolean`                                                                                                                         | ` isOrderedBroadcast() `                                                                                                                                                                      | | ` abstract void`                                                                                                                         | ` onReceive(`[Context](https://developer.android.com/reference/android/content/Context.html)` arg0, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` arg1) `   | | ` `[IBinder](https://developer.android.com/reference/android/os/IBinder.html)                                                            | ` peekService(`[Context](https://developer.android.com/reference/android/content/Context.html)` arg0, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` arg1) ` | | ` final void`                                                                                                                            | ` setDebugUnregister(boolean arg0) `                                                                                                                                                          | | ` final void`                                                                                                                            | ` setOrderedHint(boolean arg0) `                                                                                                                                                              | | ` final void`                                                                                                                            | ` setResult(int arg0, String arg1, `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)` arg2) `                                                                         | | ` final void`                                                                                                                            | ` setResultCode(int arg0) `                                                                                                                                                                   | | ` final void`                                                                                                                            | ` setResultData(String arg0) `                                                                                                                                                                | | ` final void`                                                                                                                            | ` setResultExtras(`[Bundle](https://developer.android.com/reference/android/os/Bundle.html)` arg0) `                                                                                          | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### WakefulBroadcastReceiver

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
WakefulBroadcastReceiver ()
```

<br />

<br />

## Public methods

### completeWakefulIntent

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
boolean completeWakefulIntent (Intent intent)
```

Finish the execution from a previous[startWakefulService(Context, Intent)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent)). Any wake lock that was being held will now be released.

<br />

|                                                                                                                                Parameters                                                                                                                                 ||
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `intent` | `Intent`: The Intent as originally generated by[startWakefulService(Context, Intent)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#startWakefulService(android.content.Context, android.content.Intent)). <br /> |

|                                                                           Returns                                                                           ||
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| `boolean` | 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. <br /> |

### startWakefulService

added in[version 22.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
ComponentName startWakefulService (Context context, 
                Intent intent)
```

Do a[Context.startService](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)), 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](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)), it should pass back the Intent it receives there to[completeWakefulIntent(android.content.Intent)](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver#completeWakefulIntent(android.content.Intent))in order to release the wake lock.

<br />

|                                                                                                    Parameters                                                                                                    ||
|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context` | `Context`: The Context in which it operate. <br />                                                                                                                                                    |
| `intent`  | `Intent`: The Intent with which to start the service, as per[Context.startService](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)). <br /> |

|                                               Returns                                               ||
|---------------------------------------------------------------------------------------------|--------|
| [ComponentName](https://developer.android.com/reference/android/content/ComponentName.html) | <br /> |

-

  ## Interfaces

  - [Loader.OnLoadCanceledListener](https://developer.android.com/reference/android/support/v4/content/Loader.OnLoadCanceledListener)
  - [Loader.OnLoadCompleteListener](https://developer.android.com/reference/android/support/v4/content/Loader.OnLoadCompleteListener)
-

  ## Classes

  - [AsyncTaskLoader](https://developer.android.com/reference/android/support/v4/content/AsyncTaskLoader)
  - [ContentResolverCompat](https://developer.android.com/reference/android/support/v4/content/ContentResolverCompat)
  - [ContextCompat](https://developer.android.com/reference/android/support/v4/content/ContextCompat)
  - [CursorLoader](https://developer.android.com/reference/android/support/v4/content/CursorLoader)
  - [FileProvider](https://developer.android.com/reference/android/support/v4/content/FileProvider)
  - [IntentCompat](https://developer.android.com/reference/android/support/v4/content/IntentCompat)
  - [Loader](https://developer.android.com/reference/android/support/v4/content/Loader)
  - [Loader.ForceLoadContentObserver](https://developer.android.com/reference/android/support/v4/content/Loader.ForceLoadContentObserver)
  - [LocalBroadcastManager](https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager)
  - [MimeTypeFilter](https://developer.android.com/reference/android/support/v4/content/MimeTypeFilter)
  - [PermissionChecker](https://developer.android.com/reference/android/support/v4/content/PermissionChecker)
  - [SharedPreferencesCompat](https://developer.android.com/reference/android/support/v4/content/SharedPreferencesCompat)
  - [SharedPreferencesCompat.EditorCompat](https://developer.android.com/reference/android/support/v4/content/SharedPreferencesCompat.EditorCompat)
  - [WakefulBroadcastReceiver](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver)