Worker


abstract class Worker : 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(context: Context, workerParams: WorkerParameters)

Public functions

abstract ListenableWorker.Result

Override this method to do your actual background processing.

open ForegroundInfo

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

open ListenableFuture<ForegroundInfo>

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

final ListenableFuture<ListenableWorker.Result>

Override this method to start your actual background processing.

Inherited functions

From androidx.work.ListenableWorker
Context

Gets the application android.content.Context.

UUID

Gets the ID of the WorkRequest that created this Worker.

Data

Gets the input data.

Network?
@RequiresApi(value = 28)
getNetwork()

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

@IntRange(from = 0) Int

Gets the current run attempt count for this work.

Int
@RequiresApi(value = 31)
getStopReason()

Returns a reason why this worker has been stopped.

MutableSet<String>

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

MutableList<String>

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

MutableList<Uri>

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

Boolean

Returns true if this Worker has been told to stop.

open Unit

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

ListenableFuture<Void>

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

open ListenableFuture<Void>

Updates ListenableWorker progress.

Public constructors

Worker

Added in 1.0.0
Worker(context: Context, workerParams: WorkerParameters)

Public functions

doWork

Added in 1.0.0
@WorkerThread
abstract fun doWork(): ListenableWorker.Result

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
open fun getForegroundInfo(): ForegroundInfo

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

open fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo>

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

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