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

ListenableWorker

abstract class ListenableWorker
kotlin.Any
   ↳ androidx.work.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.

Summary

Nested classes
abstract

The result of a ListenableWorker's computation.

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

Public methods
Context

Gets the application android.content.Context.

open ListenableFuture<ForegroundInfo!>

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

UUID

Gets the ID of the WorkRequest that created this Worker.

Data

Gets the input data.

Network?

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

Int

Gets the current run attempt count for this work.

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!>
setForegroundAsync(@NonNull foregroundInfo: ForegroundInfo)

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

ListenableFuture<Void!>
setProgressAsync(@NonNull data: Data)

Updates ListenableWorker progress.

abstract ListenableFuture<ListenableWorker.Result!>

Override this method to start your actual background processing.

Public constructors

<init>

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

Public methods

getApplicationContext

@NonNull fun getApplicationContext(): Context

Gets the application android.content.Context.

Return
Context The application android.content.Context

getForegroundInfoAsync

@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 android.app.NotificationManager.

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

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

getId

@NonNull fun getId()