Android 12 Developer Preview is here! Try it out, and give us your feedback!


abstract class ListenableWorker

A class that can perform work asynchronously in WorkManager. For most cases, we recommend using Worker, which offers a simple synchronous API that is executed on a pre-specified background thread.

ListenableWorker classes are instantiated at runtime by the WorkerFactory specified in the Configuration. The startWork() method is called on the main thread.

In case the work is preempted and later restarted for any reason, a new instance of ListenableWorker is created. This means that startWork is called exactly once per ListenableWorker instance. A new ListenableWorker is created if a unit of work needs to be rerun.

A ListenableWorker is given a maximum of ten minutes to finish its execution and return a Result. After this time has expired, the worker will be signalled to stop and its ListenableFuture will be cancelled.

Exercise caution when renaming or removing ListenableWorkers from your codebase.


Nested classes

The result of a ListenableWorker's computation.

Public constructors
<init>(@NonNull appContext: Context, @NonNull workerParams: WorkerParameters)

Public methods

Gets the application android.content.Context.

open ListenableFuture<ForegroundInfo!>

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


Gets the ID of the WorkRequest that created this Worker.


Gets the input data.


Gets the to use for this Worker.


Gets the current run attempt count for this work.


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


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


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


Returns true if this Worker has been told to stop.

open Unit

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

setForegroundAsync(@NonNull foregroundInfo: ForegroundInfo)

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

setProgressAsync(@NonNull data: Data)

Updates ListenableWorker progress.

abstract ListenableFuture<ListenableWorker.Result!>

Override this method to start your actual background processing.

Public constructors


    @NonNull appContext: Context,
    @NonNull workerParams: WorkerParameters)
appContext Context: The application Context
workerParams WorkerParameters: Parameters to setup the internal state of this worker

Public methods


@NonNull fun getApplicationContext(): Context

Gets the application android.content.Context.

Context The application android.content.Context


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

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

ListenableFuture<ForegroundInfo!> A ListenableFuture of ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at WorkRequest.Builder#setExpedited(OutOfQuotaPolicy).


@NonNull fun getId()