# CoroutineWorker

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

*** ** * ** ***

<br />

```
public abstract class CoroutineWorker 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.CoroutineWorker](https://developer.android.com/reference/androidx/work/CoroutineWorker) |

*** ** * ** ***

A `https://developer.android.com/reference/androidx/work/ListenableWorker` implementation that provides interop with Kotlin Coroutines. Override the `https://developer.android.com/reference/androidx/work/CoroutineWorker#doWork()` function to do your suspending work.

By default, CoroutineWorker runs on `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html` if neither `https://developer.android.com/reference/androidx/work/Configuration.Builder#setExecutor(java.util.concurrent.Executor)` or `https://developer.android.com/reference/androidx/work/Configuration.Builder#setWorkerCoroutineContext(kotlin.coroutines.CoroutineContext)` were set.

A CoroutineWorker is given a maximum of ten minutes to finish its execution and return a \[ListenableWorker.Result\]. After this time has expired, the worker will be signalled to stop.

## Summary

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

| ### 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/work/CoroutineWorker#doWork()()` A suspending method to do your work. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` | `[getCoroutineContext](https://developer.android.com/reference/androidx/work/CoroutineWorker#getCoroutineContext())()` **This method is deprecated.** use withContext(...) inside doWork() instead. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo` | `https://developer.android.com/reference/androidx/work/CoroutineWorker#getForegroundInfo()()` |
| `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/ForegroundInfo>` | `https://developer.android.com/reference/androidx/work/CoroutineWorker#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://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/CoroutineWorker#getForegroundInfoAsync()()` |
| `final void` | `https://developer.android.com/reference/androidx/work/CoroutineWorker#onStopped()()` This method is invoked when this Worker has been told to stop. |
| `final void` | `https://developer.android.com/reference/androidx/work/CoroutineWorker#setForeground(androidx.work.ForegroundInfo)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo foregroundInfo)` Makes the `https://developer.android.com/reference/androidx/work/CoroutineWorker` run in the context of a foreground `https://developer.android.com/reference/android/app/Service.html`. |
| `final void` | `https://developer.android.com/reference/androidx/work/CoroutineWorker#setProgress(androidx.work.Data)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data data)` Updates the progress for the `https://developer.android.com/reference/androidx/work/CoroutineWorker`. |
| `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/CoroutineWorker#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()()` `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

### CoroutineWorker

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

```
public CoroutineWorker(
    @NonNull Context appContext,
    @NonNull WorkerParameters params
)
```

## Public methods

### doWork

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

A suspending method to do your work.

To specify which \[CoroutineDispatcher\] your work should run on, use \`withContext()\` within \`doWork()\`. If there is no other dispatcher declared, \[Dispatchers.Default\] will be used.

A CoroutineWorker is given a maximum of ten minutes to finish its execution and return a \[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 result of the background work; note that dependent work will not execute if you return `https://developer.android.com/reference/androidx/work/ListenableWorker.Result#failure()` |

### getCoroutineContext

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

```
public @NonNull CoroutineDispatcher [getCoroutineContext](https://developer.android.com/reference/androidx/work/CoroutineWorker#getCoroutineContext())()
```

> [!CAUTION]
> **This method is deprecated.**   
> use withContext(...) inside doWork() instead.

The coroutine context on which `https://developer.android.com/reference/androidx/work/CoroutineWorker#doWork()` will run.

If this property is overridden then it takes precedent over `https://developer.android.com/reference/androidx/work/Configuration#executor()` or `https://developer.android.com/reference/androidx/work/Configuration#workerCoroutineContext()`.

By default, this is a dispatcher delegating to `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html`

### getForegroundInfo

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

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo` | The `https://developer.android.com/reference/androidx/work/ForegroundInfo` instance if the `https://developer.android.com/reference/androidx/work/WorkRequest` is marked as expedited. |

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | when not overridden. Override this method when the corresponding `https://developer.android.com/reference/androidx/work/WorkRequest` is marked expedited. |

### getForegroundInfoAsync

```
public final @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 final @NonNull ListenableFuture<@NonNull ForegroundInfo> getForegroundInfoAsync()
```

### onStopped

```
public final 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.

### setForeground

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

```
public final void setForeground(@NonNull ForegroundInfo foregroundInfo)
```

Makes the `https://developer.android.com/reference/androidx/work/CoroutineWorker` run in the context of a foreground `https://developer.android.com/reference/android/app/Service.html`. This is a suspending function unlike the `https://developer.android.com/reference/androidx/work/ListenableWorker#setForegroundAsync(androidx.work.ForegroundInfo)` API which returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`.

Calling `https://developer.android.com/reference/androidx/work/CoroutineWorker#setForeground(androidx.work.ForegroundInfo)` will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if 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/CoroutineWorker#getForegroundInfo()` instead.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/ForegroundInfo foregroundInfo` | The `https://developer.android.com/reference/androidx/work/ForegroundInfo` |

### setProgress

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

```
public final void setProgress(@NonNull Data data)
```

Updates the progress for the `https://developer.android.com/reference/androidx/work/CoroutineWorker`. This is a suspending function unlike the `https://developer.android.com/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data)` API which returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/Data data` | The progress `https://developer.android.com/reference/androidx/work/Data` |

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