Worker

public abstract class Worker
extends ListenableWorker

java.lang.Object
   ↳ androidx.work.ListenableWorker
     ↳ androidx.work.Worker


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.getExecutor()). 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 ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.

Summary

Public constructors

Worker(Context context, WorkerParameters workerParams)

Public methods

abstract ListenableWorker.Result doWork()

Override this method to do your actual background processing.

final ListenableFuture<ListenableWorker.Result> startWork()

Override this method to start your actual background processing.

Inherited methods

Public constructors

Worker

public Worker (Context context, 
                WorkerParameters workerParams)

Parameters
context Context

workerParams WorkerParameters

Public methods

doWork

public abstract ListenableWorker.Result doWork ()

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

Returns
ListenableWorker.Result The ListenableWorker.Result of the computation; note that dependent work will not execute if you use ListenableWorker.Result.failure() or ListenableWorker.Result.failure(Data)

startWork

public final ListenableFuture<ListenableWorker.Result> startWork ()

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 ListenableWorker.Result. After this time has expired, the worker will be signalled to stop and its ListenableFuture will be cancelled.

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