ListenableWorker

public abstract class ListenableWorker
extends Object

java.lang.Object
   ↳ 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 ListenableWorker.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

class ListenableWorker.Result

The result of a ListenableWorker's computation. 

Public constructors

ListenableWorker(Context appContext, WorkerParameters workerParams)

Public methods

final Context getApplicationContext()

Gets the application Context.

ListenableFuture<ForegroundInfo> getForegroundInfoAsync()

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

final UUID getId()

Gets the ID of the WorkRequest that created this Worker.

final Data getInputData()

Gets the input data.

final Network getNetwork()

Gets the Network to use for this Worker.

final int getRunAttemptCount()

Gets the current run attempt count for this work.

final Set<String> getTags()

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

final List<String> getTriggeredContentAuthorities()

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

final List<Uri> getTriggeredContentUris()

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

final boolean isStopped()

Returns true if this Worker has been told to stop.

void onStopped()

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

final ListenableFuture<Void> setForegroundAsync(ForegroundInfo foregroundInfo)

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

ListenableFuture<Void> setProgressAsync(Data data)

Updates ListenableWorker progress.

abstract ListenableFuture<ListenableWorker.Result> startWork()

Override this method to start your actual background processing.

Inherited methods

Public constructors

ListenableWorker

public ListenableWorker (Context appContext, 
                WorkerParameters workerParams)

Parameters
appContext Context: The application Context

workerParams WorkerParameters: Parameters to setup the internal state of this worker

Public methods

getApplicationContext

public final Context getApplicationContext ()

Gets the application