# ListenableWorker

Artifact: [androidx.work:work-runtime](https://developer.android.com/jetpack/androidx/releases/work) [View Source](https://cs.android.com/search?q=file:androidx/work/ListenableWorker.java+class:androidx.work.ListenableWorker) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

*** ** * ** ***


```
public abstract class ListenableWorker
```

<br />

Known direct subclasses [CoroutineWorker](https://developer.android.com/reference/androidx/work/CoroutineWorker), [RemoteListenableDelegatingWorker](https://developer.android.com/reference/androidx/work/multiprocess/RemoteListenableDelegatingWorker), [RemoteListenableWorker](https://developer.android.com/reference/androidx/work/multiprocess/RemoteListenableWorker), [RxWorker](https://developer.android.com/reference/androidx/work/RxWorker), [RxWorker](https://developer.android.com/reference/androidx/work/rxjava3/RxWorker), [Worker](https://developer.android.com/reference/androidx/work/Worker)

|---|---|
| `https://developer.android.com/reference/androidx/work/CoroutineWorker` | A `https://developer.android.com/reference/androidx/work/ListenableWorker` implementation that provides interop with Kotlin Coroutines. |
| `https://developer.android.com/reference/androidx/work/multiprocess/RemoteListenableDelegatingWorker` | A worker which can delegate to an instance of RemoteListenableWorker but importantly only constructs an instance of the RemoteListenableWorker in the remote process. |
| `https://developer.android.com/reference/androidx/work/multiprocess/RemoteListenableWorker` | Is an implementation of a `https://developer.android.com/reference/androidx/work/ListenableWorker` that can bind to a remote process. |
| `https://developer.android.com/reference/androidx/work/RxWorker` | RxJava2 interoperability Worker implementation. |
| `https://developer.android.com/reference/androidx/work/rxjava3/RxWorker` | RxJava3 interoperability Worker implementation. |
| `https://developer.android.com/reference/androidx/work/Worker` | A class that performs work synchronously on a background thread provided by `https://developer.android.com/reference/androidx/work/WorkManager`. |

Known indirect subclasses [RemoteCoroutineWorker](https://developer.android.com/reference/androidx/work/multiprocess/RemoteCoroutineWorker), [WorkManagerScheduler.SchedulerWorker](https://developer.android.com/reference/androidx/media3/exoplayer/workmanager/WorkManagerScheduler.SchedulerWorker)

|---|---|
| `https://developer.android.com/reference/androidx/work/multiprocess/RemoteCoroutineWorker` | An implementation of `https://developer.android.com/reference/androidx/work/multiprocess/RemoteListenableWorker` that can bind to a remote process. |
| `https://developer.android.com/reference/androidx/media3/exoplayer/workmanager/WorkManagerScheduler.SchedulerWorker` | A `https://developer.android.com/reference/androidx/work/Worker` that starts the target service if the requirements are met. |

*** ** * ** ***

A class that can perform work asynchronously in `https://developer.android.com/reference/androidx/work/WorkManager`. For most cases, we recommend using `https://developer.android.com/reference/androidx/work/Worker`, which offers a simple synchronous API that is executed on a pre-specified background thread.

ListenableWorker classes are instantiated at runtime by the `https://developer.android.com/reference/androidx/work/WorkerFactory` specified in the `https://developer.android.com/reference/androidx/work/Configuration`. The `https://developer.android.com/reference/androidx/work/ListenableWorker#startWork()` method is called on the main thread.

In case the work is preempted and later restarted for any reason, a new instance of ListenableWorker is created. This means that `startWork` is called exactly once per ListenableWorker instance. A new ListenableWorker is created if a unit of work needs to be rerun.

A ListenableWorker is given a maximum of ten minutes to finish its execution and return a `https://developer.android.com/reference/androidx/work/ListenableWorker.Result`. After this time has expired, the worker will be signalled to stop and its `com.google.common.util.concurrent.ListenableFuture` will be cancelled.

Exercise caution when [renaming or removing ListenableWorkers](https://developer.android.com/reference/androidx/work/WorkManager#worker_class_names) from your codebase.

## Summary

| ### Nested types |
|---|
| `public abstract class https://developer.android.com/reference/androidx/work/ListenableWorker.Result` The result of a `https://developer.android.com/reference/androidx/work/ListenableWorker`'s computation. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/work/ListenableWorker#ListenableWorker(android.content.Context,androidx.work.WorkerParameters)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html appContext, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/WorkerParameters workerParams )` |

| ### Public methods |
|---|---|
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getApplicationContext()()` Gets the application `https://developer.android.com/reference/android/content/Context.html`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getForegroundInfoAsync()()` Return an instance of `https://developer.android.com/reference/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/androidx/work/WorkRequest` is important to the user. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/UUID.html` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getId()()` Gets the ID of the `https://developer.android.com/reference/androidx/work/WorkRequest` that created this Worker. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getInputData()()` Gets the input data. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/net/Network.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 28) https://developer.android.com/reference/androidx/work/ListenableWorker#getNetwork()()` Gets the `https://developer.android.com/reference/android/net/Network.html` to use for this Worker. |
| `final @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getRunAttemptCount()()` Gets the current run attempt count for this work. |
| `final int` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 31) https://developer.android.com/reference/androidx/work/ListenableWorker#getStopReason()()` Returns a reason why this worker has been stopped. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/work/ListenableWorker#getTags()()` Gets a `https://developer.android.com/reference/java/util/Set.html` of tags associated with this Worker's `https://developer.android.com/reference/androidx/work/WorkRequest`. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/java/lang/String.html>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 24) https://developer.android.com/reference/androidx/work/ListenableWorker#getTriggeredContentAuthorities()()` Gets the list of content authorities that caused this Worker to execute. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/android/net/Uri.html>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 24) https://developer.android.com/reference/androidx/work/ListenableWorker#getTriggeredContentUris()()` Gets the list of content `https://developer.android.com/reference/android/net/Uri.html`s that caused this Worker to execute. |
| `final boolean` | `https://developer.android.com/reference/androidx/work/ListenableWorker#isStopped()()` Returns `true` if this Worker has been told to stop. |
| `void` | `https://developer.android.com/reference/androidx/work/ListenableWorker#onStopped()()` This method is invoked when this Worker has been told to stop. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/java/lang/Void.html>` | `https://developer.android.com/reference/androidx/work/ListenableWorker#setForegroundAsync(androidx.work.ForegroundInfo)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/ForegroundInfo foregroundInfo)` This specifies that the `https://developer.android.com/reference/androidx/work/WorkRequest` is long-running or otherwise important. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/java/lang/Void.html>` | `https://developer.android.com/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data data)` Updates `https://developer.android.com/reference/androidx/work/ListenableWorker` progress. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/work/ListenableWorker.Result>` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/work/ListenableWorker#startWork()()` Override this method to start your actual background processing. |

## Public constructors

### ListenableWorker

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public ListenableWorker(
    @NonNull Context appContext,
    @NonNull WorkerParameters workerParams
)
```

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html appContext` | The application `https://developer.android.com/reference/android/content/Context.html` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/WorkerParameters workerParams` | Parameters to setup the internal state of this worker |

## Public methods

### getApplicationContext

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final @NonNull Context getApplicationContext()
```

Gets the application `https://developer.android.com/reference/android/content/Context.html`.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html` | The application `https://developer.android.com/reference/android/content/Context.html` |

### getForegroundInfoAsync

Added in [2.7.0](https://developer.android.com/jetpack/androidx/releases/work#2.7.0)

```
public @NonNull ListenableFuture<ForegroundInfo> getForegroundInfoAsync()
```

Return an instance of `https://developer.android.com/reference/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/androidx/work/WorkRequest` is important to the user. In this case, WorkManager provides a signal to the OS that the process should be kept alive while this work is executing.

Prior to Android S, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, showing the notification provided in the `https://developer.android.com/reference/androidx/work/ForegroundInfo`. To update this notification subsequently, the application can use `https://developer.android.com/reference/android/app/NotificationManager.html`.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/work/ForegroundInfo>` | A `com.google.common.util.concurrent.ListenableFuture` of `https://developer.android.com/reference/androidx/work/ForegroundInfo` instance if the WorkRequest is marked immediate. For more information look at `https://developer.android.com/reference/androidx/work/WorkRequest.Builder#setExpedited(androidx.work.OutOfQuotaPolicy)`. |

### getId

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final @NonNull UUID getId()
```

Gets the ID of the `https://developer.android.com/reference/androidx/work/WorkRequest` that created this Worker.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/UUID.html` | The ID of the creating `https://developer.android.com/reference/androidx/work/WorkRequest` |

### getInputData

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final @NonNull Data getInputData()
```

Gets the input data. Note that in the case that there are multiple prerequisites for this Worker, the input data has been run through an `https://developer.android.com/reference/androidx/work/InputMerger`.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data` | The input data for this work |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/work/OneTimeWorkRequest.Builder#setInputMerger(java.lang.Class<? extends androidx.work.InputMerger>)` |   |

### getNetwork

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
@RequiresApi(value = 28)
public final @Nullable Network getNetwork()
```

Gets the `https://developer.android.com/reference/android/net/Network.html` to use for this Worker. This method returns `null` if there is no network needed for this work request.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/net/Network.html` | The `https://developer.android.com/reference/android/net/Network.html` specified by the OS to be used with this Worker |

### getRunAttemptCount

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final @IntRange(from = 0) int getRunAttemptCount()
```

Gets the current run attempt count for this work. Note that for periodic work, this value gets reset between periods.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int` | The current run attempt count for this work. |

### getStopReason

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/work#2.9.0)

```
@RequiresApi(value = 31)
public final int getStopReason()
```

Returns a reason why this worker has been stopped. Return values match values of `JobParameters.STOP_REASON_*` constants, e.g. `https://developer.android.com/reference/android/app/job/JobParameters.html#STOP_REASON_CONSTRAINT_CHARGING` or `https://developer.android.com/reference/androidx/work/WorkInfo#STOP_REASON_UNKNOWN()`

If a worker hasn't been stopped, `https://developer.android.com/reference/androidx/work/WorkInfo#STOP_REASON_NOT_STOPPED()` is returned.

Value is one of the following:

- `-512`
- `-256`
- `-128`
- `1`
- `2`
- `3`
- `4`
- `5`
- `6`
- `7`
- `8`
- `9`
- `10`
- `11`
- `12`
- `13`
- `14`
- `15`

<br />

### getTags

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final @NonNull Set<String> getTags()
```

Gets a `https://developer.android.com/reference/java/util/Set.html` of tags associated with this Worker's `https://developer.android.com/reference/androidx/work/WorkRequest`.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html>` | The `https://developer.android.com/reference/java/util/Set.html` of tags associated with this Worker's `https://developer.android.com/reference/androidx/work/WorkRequest` |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/work/WorkRequest.Builder#addTag(java.lang.String)` |   |

### getTriggeredContentAuthorities

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
@RequiresApi(value = 24)
public final @NonNull List<String> getTriggeredContentAuthorities()
```

Gets the list of content authorities that caused this Worker to execute. See `JobParameters#getTriggeredContentAuthorities()` for relevant `JobScheduler` code.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/java/lang/String.html>` | The list of content authorities that caused this Worker to execute |

### getTriggeredContentUris

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
@RequiresApi(value = 24)
public final @NonNull List<Uri> getTriggeredContentUris()
```

Gets the list of content `https://developer.android.com/reference/android/net/Uri.html`s that caused this Worker to execute. See `JobParameters#getTriggeredContentUris()` for relevant `JobScheduler` code.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/android/net/Uri.html>` | The list of content `https://developer.android.com/reference/android/net/Uri.html`s that caused this Worker to execute |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/work/Constraints.Builder#addContentUriTrigger(android.net.Uri,boolean)` |   |

### isStopped

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public final boolean isStopped()
```

Returns `true` if this Worker has been told to stop. This could be because of an explicit cancellation signal by the user, or because the system has decided to preempt the task. In these cases, the results of the work will be ignored by WorkManager and it is safe to stop the computation. WorkManager will retry the work at a later time if necessary.

| Returns |
|---|---|
| `boolean` | `true` if the work operation has been interrupted |

### onStopped

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public void onStopped()
```

This method is invoked when this Worker has been told to stop. At this point, the `com.google.common.util.concurrent.ListenableFuture` returned by the instance of `https://developer.android.com/reference/androidx/work/ListenableWorker#startWork()` is also cancelled. This could happen due to an explicit cancellation signal by the user, or because the system has decided to preempt the task. In these cases, the results of the work will be ignored by WorkManager. All processing in this method should be lightweight - there are no contractual guarantees about which thread will invoke this call, so this should not be a long-running or blocking operation.

### setForegroundAsync

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/work#2.3.0)

```
public final @NonNull ListenableFuture<Void> setForegroundAsync(@NonNull ForegroundInfo foregroundInfo)
```

This specifies that the `https://developer.android.com/reference/androidx/work/WorkRequest` is long-running or otherwise important. In this case, WorkManager provides a signal to the OS that the process should be kept alive if possible while this work is executing.

Calls to `setForegroundAsync` \*must\* complete before a `https://developer.android.com/reference/androidx/work/ListenableWorker` signals completion by returning a `https://developer.android.com/reference/androidx/work/ListenableWorker.Result`.

Under the hood, WorkManager manages and runs a foreground service on your behalf to execute this WorkRequest, showing the notification provided in `https://developer.android.com/reference/androidx/work/ForegroundInfo`.

Calling `setForegroundAsync` will fail with an `https://developer.android.com/reference/java/lang/IllegalStateException.html` when the process is subject to foreground service restrictions. Consider using `https://developer.android.com/reference/androidx/work/WorkRequest.Builder#setExpedited(androidx.work.OutOfQuotaPolicy)` and `https://developer.android.com/reference/androidx/work/ListenableWorker#getForegroundInfoAsync()` instead.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/ForegroundInfo foregroundInfo` | The `https://developer.android.com/reference/androidx/work/ForegroundInfo` |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/java/lang/Void.html>` | A `com.google.common.util.concurrent.ListenableFuture` which resolves after the `https://developer.android.com/reference/androidx/work/ListenableWorker` transitions to running in the context of a foreground `https://developer.android.com/reference/android/app/Service.html`. |

### setProgressAsync

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/work#2.3.0)

```
public @NonNull ListenableFuture<Void> setProgressAsync(@NonNull Data data)
```

Updates `https://developer.android.com/reference/androidx/work/ListenableWorker` progress.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data data` | The progress `https://developer.android.com/reference/androidx/work/Data` |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/java/lang/Void.html>` | A `com.google.common.util.concurrent.ListenableFuture` which resolves after progress is persisted. Cancelling this future is a no-op. |

### startWork

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
@MainThread
public abstract @NonNull ListenableFuture<ListenableWorker.Result> startWork()
```

Override this method to start your actual background processing. This method is called on the main thread.

A ListenableWorker has a well defined [execution window](https://d.android.com/reference/android/app/job/JobScheduler) to to finish its execution and return a `https://developer.android.com/reference/androidx/work/ListenableWorker.Result`. After this time has expired, the worker will be signalled to stop and its `com.google.common.util.concurrent.ListenableFuture` will be cancelled.

The future will also be cancelled if this worker is stopped for any reason (see `https://developer.android.com/reference/androidx/work/ListenableWorker#onStopped()`).

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/work/ListenableWorker.Result>` | A `com.google.common.util.concurrent.ListenableFuture` with the `https://developer.android.com/reference/androidx/work/ListenableWorker.Result` of the computation. If you cancel this Future, WorkManager will treat this unit of work as failed. |