# 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 />

```
abstract class Worker : ListenableWorker
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.work.ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker) ||
|   | ↳ | [androidx.work.Worker](https://developer.android.com/reference/kotlin/androidx/work/Worker) |

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

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/workmanager/WorkManagerScheduler.SchedulerWorker` | A `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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/kotlin/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/kotlin/androidx/work/ListenableWorker.Result`. After this time has expired, the Worker will be signalled to stop.

## Summary

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

| ### Public functions |
|---|---|
| `abstract https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/work/Worker#doWork()()` Override this method to do your actual background processing. |
| `open https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` | `@https://developer.android.com/reference/kotlin/androidx/annotation/WorkerThread https://developer.android.com/reference/kotlin/androidx/work/Worker#getForegroundInfo()()` An instance of `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/kotlin/androidx/work/WorkRequest` is important to the user. |
| `open https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/kotlin/androidx/work/Worker#getForegroundInfoAsync()()` Return an instance of `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/kotlin/androidx/work/WorkRequest` is important to the user. |
| `final https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result>` | `https://developer.android.com/reference/kotlin/androidx/work/Worker#startWork()()` Override this method to start your actual background processing. |

| ### Public properties |
|---|---|
| `open https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/kotlin/androidx/work/Worker#foregroundInfoAsync()` |

### Inherited functions

From [androidx.work.ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker) `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#onStopped()()`

This method is invoked when this Worker has been told to stop.
`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/kotlin/androidx/work/ListenableWorker#setForegroundAsync(androidx.work.ForegroundInfo)(foregroundInfo: https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo)`

This specifies that the `https://developer.android.com/reference/kotlin/androidx/work/WorkRequest` is long-running or otherwise important.
`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/kotlin/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data)(data: https://developer.android.com/reference/kotlin/androidx/work/Data)`

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

### Inherited properties

From [androidx.work.ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker) `https://developer.android.com/reference/android/content/Context.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#applicationContext()` `https://developer.android.com/reference/java/util/UUID.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#id()` `https://developer.android.com/reference/kotlin/androidx/work/Data` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#inputData()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#isStopped()` `https://developer.android.com/reference/android/net/Network.html?` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#network()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#runAttemptCount()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#stopReason()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#tags()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#triggeredContentAuthorities()` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html<https://developer.android.com/reference/android/net/Uri.html>` `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker#triggeredContentUris()`

## Public constructors

### Worker

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

```
Worker(context: Context, workerParams: WorkerParameters)
```

## Public functions

### doWork

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

```
@WorkerThread
abstract fun doWork(): ListenableWorker.Result
```

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/kotlin/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/kotlin/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/kotlin/androidx/work/ListenableWorker.Result`. After this time has expired, the Worker will be signalled to stop.

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

### getForegroundInfo

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

```
@WorkerThread
open fun getForegroundInfo(): ForegroundInfo
```

An instance of `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/androidx/work/ForegroundInfo` | A `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` instance if the WorkRequest is marked immediate. For more information look at `https://developer.android.com/reference/kotlin/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

```
open fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo>
```

Return an instance of `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` if the `https://developer.android.com/reference/kotlin/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/kotlin/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://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo>` | A `com.google.common.util.concurrent.ListenableFuture` of `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` instance if the WorkRequest is marked immediate. For more information look at `https://developer.android.com/reference/kotlin/androidx/work/WorkRequest.Builder#setExpedited(androidx.work.OutOfQuotaPolicy)`. |

### startWork

```
final fun startWork(): ListenableFuture<ListenableWorker.Result>
```

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/kotlin/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/kotlin/androidx/work/ListenableWorker#onStopped()`).

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

## Public properties

### foregroundInfoAsync

```
open val foregroundInfoAsync: ListenableFuture<ForegroundInfo>
```