Worker

abstract class Worker : ListenableWorker
kotlin.Any
   ↳ 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 androidx.work.ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.

Summary

Public constructors

<init>(@NonNull context: Context, @NonNull workerParams: WorkerParameters)

Public methods

abstract ListenableWorker.Result

Override this method to do your actual background processing.

ListenableFuture<ListenableWorker.Result!>

Inherited functions

Public constructors

<init>

Worker(@NonNull context: Context, @NonNull workerParams: WorkerParameters)

Public methods

doWork

@WorkerThread @NonNull abstract fun doWork(): ListenableWorker.Result

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

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

startWork

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