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

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

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker` implementation that provides interop with Kotlin Coroutines. Override the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/work/Configuration.Builder#setExecutor(java.util.concurrent.Executor)` or `https://developer.android.com/reference/kotlin/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/kotlin/androidx/work/CoroutineWorker#CoroutineWorker(android.content.Context,androidx.work.WorkerParameters)(appContext: https://developer.android.com/reference/android/content/Context.html, params: https://developer.android.com/reference/kotlin/androidx/work/WorkerParameters)` |

| ### Public functions |
|---|---|
| `abstract suspend https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker.Result` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#doWork()()` A suspending method to do your work. |
| `open suspend https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#getForegroundInfo()()` |
| `final 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/CoroutineWorker#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://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#onStopped()()` This method is invoked when this Worker has been told to stop. |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#setForeground(androidx.work.ForegroundInfo)(foregroundInfo: https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo)` Makes the `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker` run in the context of a foreground `https://developer.android.com/reference/android/app/Service.html`. |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#setProgress(androidx.work.Data)(data: https://developer.android.com/reference/kotlin/androidx/work/Data)` Updates the progress for the `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker`. |
| `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/CoroutineWorker#startWork()()` Override this method to start your actual background processing. |

| ### Public properties |
|---|---|
| `open https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-dispatcher/index.html` | `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#coroutineContext()` **This property is deprecated.** use withContext(...) inside doWork() instead. |
| `final 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/CoroutineWorker#foregroundInfoAsync()` |

### Inherited functions

From [androidx.work.ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker) `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

### CoroutineWorker

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

```
CoroutineWorker(appContext: Context, params: WorkerParameters)
```

## Public functions

### doWork

```
abstract suspend fun doWork(): ListenableWorker.Result
```

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/kotlin/androidx/work/ListenableWorker.Result` | The `https://developer.android.com/reference/kotlin/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/kotlin/androidx/work/ListenableWorker.Result#failure()` |

### getForegroundInfo

```
open suspend fun getForegroundInfo(): ForegroundInfo
```

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` | The `https://developer.android.com/reference/kotlin/androidx/work/ForegroundInfo` instance if the `https://developer.android.com/reference/kotlin/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/kotlin/androidx/work/WorkRequest` is marked expedited. |

### getForegroundInfoAsync

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

### onStopped

```
final fun onStopped(): Unit
```

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/kotlin/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)

```
suspend fun setForeground(foregroundInfo: ForegroundInfo): Unit
```

Makes the `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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/kotlin/androidx/work/WorkRequest.Builder#setExpedited(androidx.work.OutOfQuotaPolicy)` and `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker#getForegroundInfo()` instead.

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

### setProgress

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

```
suspend fun setProgress(data: Data): Unit
```

Updates the progress for the `https://developer.android.com/reference/kotlin/androidx/work/CoroutineWorker`. This is a suspending function unlike the `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `data: https://developer.android.com/reference/kotlin/androidx/work/Data` | The progress `https://developer.android.com/reference/kotlin/androidx/work/Data` |

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

### coroutineContext

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)

```
open val coroutineContext: CoroutineDispatcher
```

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

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

If this property is overridden then it takes precedent over `https://developer.android.com/reference/kotlin/androidx/work/Configuration#executor()` or `https://developer.android.com/reference/kotlin/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`

### foregroundInfoAsync

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