Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

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.

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.

Data

Gets the input data.

MutableList<String!>

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

abstract ListenableFuture<ListenableWorker.Result!>

Override this method to start your actual background processing.

UUID

Gets the ID of the WorkRequest that created this Worker.

Boolean

Returns true if this Worker has been told to stop.

Int

Gets the current run attempt count for this work.

Network?

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

open Unit

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

MutableSet<String!>

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

MutableList<Uri!>

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

Public constructors

<init>

ListenableWorker(@NonNull appContext: Context, @NonNull workerParams: WorkerParameters)
Parameters
appContext Context: The application Context
workerParams Context: 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

getInputData

@NonNull fun getInputData(): Data

Gets the input data. Note that in the case that there are multiple prerequisites for this Worker, the input data has been run through an InputMerger.

Return
Data: The input data for this work

getTriggeredContentAuthorities

@NonNull fun getTriggeredContentAuthorities(): MutableList<String!>

Gets the list of content authorities that caused this Worker to execute. See JobParameters#getTriggeredContentAuthorities() for relevant JobScheduler code.

Return
MutableList<String!>: The list of content authorities that caused this Worker to execute

startWork

@MainThread @NonNull abstract fun startWork(): ListenableFuture<ListenableWorker.Result!>

Override this method to start your actual background processing. This method is called on the main thread.

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.

Return
ListenableFuture<ListenableWorker.Result!>: A ListenableFuture with the Result of the computation. If you cancel this Future, WorkManager will treat this unit of work as failed.

getId

@NonNull fun getId(): UUID

Gets the ID of the WorkRequest that created this Worker.

Return
UUID: The ID of the creating WorkRequest

isStopped

fun isStopped(): Boolean

Returns true if this Worker has been told to stop. This could be because of an explicit cancellation signal by the user, or because the system has decided to preempt the task. In these cases, the results of the work will be ignored by WorkManager and it is safe to stop the computation. WorkManager will retry the work at a later time if necessary.

Return
Boolean: true if the work operation has been interrupted

getRunAttemptCount

fun getRunAttemptCount(): Int

Gets the current run attempt count for this work. Note that for periodic work, this value gets reset between periods.

Return
Int: The current run attempt count for this work.

getNetwork

@Nullable fun getNetwork(): Network?

Gets the android.net.Network to use for this Worker. This method returns null if there is no network needed for this work request.

Return
Network?: The android.net.Network specified by the OS to be used with this Worker

onStopped

open fun onStopped(): Unit

This method is invoked when this Worker has been told to stop. This could happen due to an explicit cancellation signal by the user, or because the system has decided to preempt the task. In these cases, the results of the work will be ignored by WorkManager. All processing in this method should be lightweight - there are no contractual guarantees about which thread will invoke this call, so this should not be a long-running or blocking operation.

getTags

@NonNull fun getTags(): MutableSet<String!>

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

Return
MutableSet<String!>: The java.util.Set of tags associated with this Worker's WorkRequest

getTriggeredContentUris

@NonNull fun getTriggeredContentUris(): MutableList<Uri!>

Gets the list of content android.net.Uris that caused this Worker to execute. See JobParameters#getTriggeredContentUris() for relevant JobScheduler code.

Return
MutableList<Uri!>: The list of content android.net.Uris that caused this Worker to execute