Worker

public abstract class Worker extends ListenableWorker

Known direct subclasses
WorkManagerScheduler.SchedulerWorker

A Worker that starts the target service if the requirements are met.


A class that performs work synchronously on a background thread provided by WorkManager.

Worker classes are instantiated at runtime by WorkManager and the .doWork method is called on a pre-specified background thread (see 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 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 androidx.work.ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.

Summary

Public constructors

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

Public methods

abstract @NonNull ListenableWorker.Result

Override this method to do your actual background processing.

@NonNull ForegroundInfo

An instance of ForegroundInfo if the WorkRequest is important to the user.

@NonNull ListenableFuture<@NonNull ForegroundInfo>

Return an instance of ForegroundInfo if the WorkRequest is important to the user.

final @NonNull ListenableFuture<@NonNull ListenableWorker.Result>

Override this method to start your actual background processing.

Inherited methods

From androidx.work.ListenableWorker
final @NonNull Context

Gets the application android.content.Context.

final @NonNull UUID

Gets the ID of the WorkRequest that created this Worker.

final @NonNull Data

Gets the input data.

final @Nullable Network
@RequiresApi(value = 28)
getNetwork()

Gets the android.net.Network to use for this Worker.

final @IntRange(from = 0) int

Gets the current run attempt count for this work.

final int

Returns a reason why this worker has been stopped.

final @NonNull Set<@NonNull String>

Gets a java.util.Set of tags associated with this Worker's WorkRequest.

final @NonNull List<@NonNull String>

Gets the list of content authorities that caused this Worker to execute.

final @NonNull List<@NonNull Uri>

Gets the list of content android.net.Uris that caused this Worker to execute.

final boolean

Returns true if this Worker has been told to stop.

void

This method is invoked when this Worker has been told to stop.

final @NonNull ListenableFuture<@NonNull Void>

This specifies that the WorkRequest is long-running or otherwise important.

@NonNull ListenableFuture<@NonNull Void>

Updates ListenableWorker progress.

Public constructors

Worker

Added in 1.0.0
public Worker(@NonNull Context context, @NonNull WorkerParameters workerParams)

Public methods

doWork

Added in 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 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 ListenableWorker.

A Worker has a well defined execution window to finish its execution and return a androidx.work.ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.

getForegroundInfo

Added in 2.8.0
@WorkerThread
public @NonNull ForegroundInfo getForegroundInfo()

An instance of ForegroundInfo if the 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 ForegroundInfo. To update this notification subsequently, the application can use android.app.NotificationManager.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

Returns
@NonNull ForegroundInfo

A ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at WorkRequest.Builder.setExpedited.

Throws
kotlin.IllegalStateException

if it is not overridden and worker tries to go to foreground

getForegroundInfoAsync

public @NonNull ListenableFuture<@NonNull ForegroundInfogetForegroundInfoAsync()

Return an instance of ForegroundInfo if the 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 ForegroundInfo. To update this notification subsequently, the application can use android.app.NotificationManager.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

Returns
@NonNull ListenableFuture<@NonNull ForegroundInfo>

A com.google.common.util.concurrent.ListenableFuture of ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at setExpedited.

startWork

public final @NonNull ListenableFuture<@NonNull ListenableWorker.ResultstartWork()

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 to to finish its execution and return a 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 onStopped).

Returns
@NonNull ListenableFuture<@NonNull ListenableWorker.Result>

A com.google.common.util.concurrent.ListenableFuture with the Result of the computation. If you cancel this Future, WorkManager will treat this unit of work as failed.