Stay organized with collections Save and categorize content based on your preferences.

Worker

public abstract class Worker extends ListenableWorker


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 getExecutor). 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

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

@NonNull ListenableFuture<ForegroundInfo>

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

final @NonNull ListenableFuture<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 @NonNull Set<String>

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

final @NonNull List<String>

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

final @NonNull List<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<Void>

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

@NonNull ListenableFuture<Void>

Updates ListenableWorker progress.

Public constructors

Worker

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

Public methods

doWork

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

Returns
@NonNull ListenableWorker.Result

The androidx.work.ListenableWorker.Result of the computation; note that dependent work will not execute if you use failure or failure

getForegroundInfo

@WorkerThread
public @NonNull ForegroundInfo getForegroundInfo()

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 ForegroundInfo

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

getForegroundInfoAsync

public @NonNull ListenableFuture<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<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<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<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.