Worker

abstract class Worker
kotlin.Any
   ↳ androidx.work.Worker

The basic unit of work.

Summary

Nested classes

Public constructors

Public methods
Context

Data

Array<String!>?

Unit
setOutputData(outputData: Data)

Call this method to pass an Data object to Worker that is dependent on this one.

UUID

Gets the ID of the WorkRequest that created this Worker.

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.

Data

abstract Worker.WorkerResult

Override this method to do your actual background processing.

MutableSet<String!>

Array<Uri!>?

Public constructors

<init>

Worker()

Public methods

getApplicationContext

fun getApplicationContext(): Context

getInputData

fun getInputData(): Data

getTriggeredContentAuthorities

fun getTriggeredContentAuthorities(): Array<String!>?

setOutputData

fun setOutputData(outputData: Data): Unit

Call this method to pass an Data object to Worker that is dependent on this one. Note that if there are multiple Workers that contribute to the target, the Data will be merged together, so it is up to the developer to make sure that keys are unique. New values and types will clobber old values and types, and if there are multiple parent Workers of a child Worker, the order of clobbering may not be deterministic. This method is invoked after #doWork() returns Worker.WorkerResult#SUCCESS and there are chained jobs available. For example, if you had this structure: WorkManager.getInstance(context) .enqueueWithDefaults(WorkerA.class, WorkerB.class) .then(WorkerC.class) .enqueue() This method would be called for both WorkerA and WorkerB after their successful completion, modifying the input Data for WorkerC.

Parameters
outputData Data: An Data object that will be merged into the input Data of any OneTimeWorkRequest that is dependent on this one, or null if there is nothing to contribute

getId

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.

Return
Boolean: true if the work operation has been interrupted

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.

getOutputData

fun getOutputData(): Data

doWork

abstract fun doWork(): Worker.WorkerResult

Override this method to do your actual background processing.

Return
Worker.WorkerResult: The result of the work, corresponding to a WorkerResult value. If a different value is returned, the result shall be defaulted to Worker.WorkerResult#FAILURE.

getTags

fun getTags(): MutableSet<String!>

getTriggeredContentUris

fun getTriggeredContentUris(): Array<Uri!>?