# Worker

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

*** ** * ** ***

<br />

```
public abstract class Worker extends ListenableWorker
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.work.ListenableWorker](https://developer.android.com/reference/androidx/work/ListenableWorker) ||
|   | ↳ | [androidx.work.Worker](https://developer.android.com/reference/androidx/work/Worker) |

Known direct subclasses [WorkManagerScheduler.SchedulerWorker](https://developer.android.com/reference/androidx/media3/exoplayer/workmanager/WorkManagerScheduler.SchedulerWorker)

|---|---|
| `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 performs work synchronously on a background thread provided by `https://developer.android.com/reference/androidx/work/WorkManager`.

Worker classes are instantiated at runtime by WorkManager and the .doWork method is called on a pre-specified background thread (see `https://developer.android.com/reference/androidx/work/Configuration#executor()`). This method is for **synchronous** processing of your work, meaning that once you return from that method, the Worker is considered to be finished and will be destroyed. If you need to do your work asynchronously or call asynchronous APIs, you should use `https://developer.android.com/reference/androidx/work/ListenableWorker`.

In case the work is preempted for any reason, the same instance of Worker is not reused. This means that .doWork is called exactly once per Worker instance. A new Worker is created if a unit of work needs to be rerun.

A Worker 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.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/work/Worker#Worker(android.content.Context,androidx.work.WorkerParameters)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/WorkerParameters workerParams)` |

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ListenableWorker.Result` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread https://developer.android.com/reference/androidx/work/Worker#doWork()()` Override this method to do your actual background processing. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread https://developer.android.com/reference/androidx/work/Worker#getForegroundInfo()()` 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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/androidx/work/Worker#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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/androidx/work/Worker#getForegroundInfoAsync()()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ListenableWorker.Result>` | `https://developer.android.com/reference/androidx/work/Worker#startWork()()` Override this method to start your actual background processing. |

### Inherited methods

From [androidx.work.ListenableWorker](https://developer.android.com/reference/androidx/work/ListenableWorker) `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()()` `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()()` `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()()` `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()()` `final @https://developer.android.com/reference/androidx/annotation/IntRange(from = 0) int` `https://developer.android.com/reference/androidx/work/ListenableWorker#getRunAttemptCount()()` `final int` `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 31)
https://developer.android.com/reference/androidx/work/ListenableWorker#getStopReason()()` `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/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html>` `https://developer.android.com/reference/androidx/work/ListenableWorker#getTags()()` `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/androidx/annotation/NonNull 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()()` `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/androidx/annotation/NonNull 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()()` `final boolean` `https://developer.android.com/reference/androidx/work/ListenableWorker#isStopped()()` `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/androidx/annotation/NonNull 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/androidx/annotation/NonNull 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.

## Public constructors

### Worker

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

```
public Worker(@NonNull Context context, @NonNull WorkerParameters workerParams)
```

## Public methods

### doWork

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

```
@WorkerThread
public abstract @NonNull ListenableWorker.Result doWork()
```

Override this method to do your actual background processing. This method is called on a background thread - you are required to **synchronously** do your work and return the `https://developer.android.com/reference/androidx/work/ListenableWorker.Result` from this method. Once you return from this method, the Worker is considered to have finished what its doing and will be destroyed. If you need to do your work asynchronously on a thread of your own choice, see `https://developer.android.com/reference/androidx/work/ListenableWorker`.

A Worker has a well defined [execution window](https://d.android.com/reference/android/app/job/JobScheduler) 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.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ListenableWorker.Result` | The `https://developer.android.com/reference/androidx/work/ListenableWorker.Result` of the computation; note that dependent work will not execute if you use `https://developer.android.com/reference/androidx/work/ListenableWorker.Result#failure()` or `https://developer.android.com/reference/androidx/work/ListenableWorker.Result#failure()` |

### getForegroundInfo

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

```
@WorkerThread
public @NonNull ForegroundInfo getForegroundInfo()
```

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://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo` | A `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)`. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | if it is not overridden and worker tries to go to foreground |

### getForegroundInfoAsync

```
public @NonNull ListenableFuture<@NonNull 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://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull 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)`. |

### getForegroundInfoAsync

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

### startWork

```
public final @NonNull ListenableFuture<@NonNull 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://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull 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. |