# RxWorker

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

*** ** * ** ***

<br />

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

*** ** * ** ***

RxJava2 interoperability Worker implementation.

When invoked by the `https://developer.android.com/reference/androidx/work/WorkManager`, it will call @`https://developer.android.com/reference/androidx/work/RxWorker#createWork()` to get a `Single<Result>` subscribe to it.

By default, RxWorker will subscribe on the thread pool that runs `https://developer.android.com/reference/androidx/work/WorkManager``https://developer.android.com/reference/androidx/work/Worker`s. You can change this behavior by overriding `https://developer.android.com/reference/androidx/work/RxWorker#getBackgroundScheduler()` method.

An RxWorker 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.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/work/Worker` |   |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/work/RxWorker#RxWorker(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 |
|---|---|
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.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/RxWorker#createWork()()` Override this method to define your actual work and return a `Single` of `https://developer.android.com/reference/androidx/work/ListenableWorker.Result` which will be subscribed by the `https://developer.android.com/reference/androidx/work/WorkManager`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/androidx/work/ForegroundInfo>` | `https://developer.android.com/reference/androidx/work/RxWorker#getForegroundInfo()()` Return an `Single` with 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://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/RxWorker#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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` | `https://developer.android.com/reference/androidx/work/RxWorker#setCompletableProgress(androidx.work.Data)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data data)` Updates the progress for a `https://developer.android.com/reference/androidx/work/RxWorker`. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` | `https://developer.android.com/reference/androidx/work/RxWorker#setForeground(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. |
| `final @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/java/lang/Void.html>` | `[setProgress](https://developer.android.com/reference/androidx/work/RxWorker#setProgress(androidx.work.Data))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Data data)` **This method is deprecated.** This method is being deprecated because it is impossible to signal success via a \`Single \` type. <br /> |
| `@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/work/RxWorker#startWork()()` Override this method to start your actual background processing. |

| ### Protected methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Scheduler.html` | `https://developer.android.com/reference/androidx/work/RxWorker#getBackgroundScheduler()()` Returns the default background scheduler that `RxWorker` will use to subscribe. |

### 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()()`

Gets the application `https://developer.android.com/reference/android/content/Context.html`.
`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.

## Public constructors

### RxWorker

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

```
public RxWorker(
    @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

### createWork

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

```
@MainThread
public abstract @NonNull Single<ListenableWorker.Result> createWork()
```

Override this method to define your actual work and return a `Single` of `https://developer.android.com/reference/androidx/work/ListenableWorker.Result` which will be subscribed by the `https://developer.android.com/reference/androidx/work/WorkManager`.

If the returned `Single` fails, the worker will be considered as failed.

If the `https://developer.android.com/reference/androidx/work/RxWorker` is cancelled by the `https://developer.android.com/reference/androidx/work/WorkManager` (e.g. due to a constraint change), `https://developer.android.com/reference/androidx/work/WorkManager` will dispose the subscription immediately.

By default, subscription happens on the shared `https://developer.android.com/reference/androidx/work/Worker` pool. You can change it by overriding `https://developer.android.com/reference/androidx/work/RxWorker#getBackgroundScheduler()`.

An RxWorker 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.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/androidx/work/ListenableWorker.Result>` | a `Single<Result>` that represents the work. |

### getForegroundInfo

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

```
public @NonNull Single<ForegroundInfo> getForegroundInfo()
```

Return an `Single` with 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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/androidx/work/ForegroundInfo>` | A `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html` 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<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)`. |

### setCompletableProgress

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

```
public final @NonNull Completable setCompletableProgress(@NonNull Data data)
```

Updates the progress for a `https://developer.android.com/reference/androidx/work/RxWorker`. This method returns a `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` unlike the `https://developer.android.com/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data)` API.

| 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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` | The `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` |

### setForeground

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

```
public final @NonNull Completable setForeground(@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/RxWorker` 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 `setForeground` 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/RxWorker#getForegroundInfo()` 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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` | A `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` which resolves after the `https://developer.android.com/reference/androidx/work/RxWorker` transitions to running in the context of a foreground `https://developer.android.com/reference/android/app/Service.html`. |

### setProgress

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

```
public final @NonNull Single<Void> [setProgress](https://developer.android.com/reference/androidx/work/RxWorker#setProgress(androidx.work.Data))(@NonNull Data data)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> This method is being deprecated because it is impossible to signal success via a \`Single \` type. A `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html` should have been used.
>
> Use `https://developer.android.com/reference/androidx/work/RxWorker#setCompletableProgress(androidx.work.Data)` instead.

Updates the progress for a `https://developer.android.com/reference/androidx/work/RxWorker`. This method returns a `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html` unlike the `https://developer.android.com/reference/androidx/work/ListenableWorker#setProgressAsync(androidx.work.Data)` API.

This method is deprecated. Use `https://developer.android.com/reference/androidx/work/RxWorker#setCompletableProgress(androidx.work.Data)` instead.

| 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://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html<https://developer.android.com/reference/java/lang/Void.html>` | The `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html` |

### startWork

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

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

## Protected methods

### getBackgroundScheduler

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

```
protected @NonNull Scheduler getBackgroundScheduler()
```

Returns the default background scheduler that `RxWorker` will use to subscribe.

The default implementation returns a Scheduler that uses the `https://developer.android.com/reference/java/util/concurrent/Executor.html` which was provided in `https://developer.android.com/reference/androidx/work/WorkManager`'s `https://developer.android.com/reference/androidx/work/Configuration` (or the default one it creates).

You can override this method to change the Scheduler used by RxWorker to start its subscription. It always observes the result of the `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html` in WorkManager's internal thread.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Scheduler.html` | The default `https://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Scheduler.html`. |