Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.

NonBlockingWorker

public abstract class NonBlockingWorker
extends Object

java.lang.Object
   ↳ androidx.work.NonBlockingWorker


The basic object that performs work. Worker classes are instantiated at runtime by WorkManager and the onStartWork method is called on the background thread. In case the work is preempted for any reason, the same instance of NonBlockingWorker is not reused. This means that onStartWork is called exactly once per NonBlockingWorker instance. The NonBlockingWorker signals work completion by using a WorkFinishedCallback.

Summary

Public constructors

NonBlockingWorker()

Public methods

final Context getApplicationContext()

Gets the application Context.

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.

Data getOutputData()
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 String[] getTriggeredContentAuthorities()

Gets the array of content authorities that caused this Worker to execute

final Uri[] getTriggeredContentUris()

Gets the array of content Uris that caused this Worker to execute

final boolean isCancelled()

Returns true if this Worker has been told to stop and explicitly informed that it is cancelled and will never execute again.

final boolean isStopped()

Returns true if this Worker has been told to stop.

void onStopped(boolean cancelled)

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

void setOutputData(Data outputData)

Call this method to pass a Data object as the output of this Worker.

Protected methods

void internalInit(Context appContext, UUID id, Extras extras)

Inherited methods

Public constructors

NonBlockingWorker

public NonBlockingWorker ()

Public methods

getApplicationContext

public final Context getApplicationContext ()

Gets the application Context.

Returns
Context The application Context

getId

public final UUID getId ()

Gets the ID of the WorkRequest that created this Worker.

Returns
UUID The ID of the creating WorkRequest

getInputData

public final Data getInputData ()

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.

Returns
Data The input data for this work

getNetwork

public final Network getNetwork ()

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

Returns
Network The Network specified by the OS to be used with this Worker

getOutputData

public Data getOutputData ()

Returns
Data the output Data set by the Worker.

getRunAttemptCount

public final int getRunAttemptCount ()

Gets the current run attempt count for this work.

Returns
int The current run attempt count for this work.

getTags

public final Set<String> getTags ()

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

Returns
Set<String> The Set of tags associated with this Worker's WorkRequest

getTriggeredContentAuthorities

public final String[] getTriggeredContentAuthorities ()

Gets the array of content authorities that caused this Worker to execute

Returns
String[] The array of content authorities that caused this Worker to execute

getTriggeredContentUris

public final Uri[] getTriggeredContentUris ()

Gets the array of content Uris that caused this Worker to execute

Returns
Uri[] The array of content Uris that caused this Worker to execute

isCancelled

public final boolean isCancelled ()

Returns true if this Worker has been told to stop and explicitly informed that it is cancelled and will never execute again. If isStopped() returns true but this method returns false, that means the system has decided to preempt the task.

Note that it is almost never sufficient to check only this method; its value is only meaningful when isStopped() returns true.

Returns
boolean true if this work operation has been cancelled

isStopped

public final boolean isStopped ()

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.

Returns
boolean true if the work operation has been interrupted

onStopped

public void onStopped (boolean cancelled)

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.

Parameters
cancelled boolean: If true, the work has been explicitly cancelled

setOutputData

public void setOutputData (Data outputData)

Call this method to pass a Data object as the output of this Worker. This result can be observed and passed to Workers that are dependent on this one. In cases like where two or more OneTimeWorkRequests share a dependent WorkRequest, their Data will be merged together using an InputMerger. The default InputMerger is OverwritingInputMerger, unless otherwise specified using the OneTimeWorkRequest.Builder.setInputMerger(Class) method.

This method is invoked after onStartWork and returns Worker.Result.SUCCESS or a Worker.Result.FAILURE.

For example, if you had this structure:

 WorkManager.getInstance(context)
             .beginWith(workRequestA, workRequestB)
             .then(workRequestC)
             .enqueue()
This method would be called for both workRequestA and workRequestB after their completion, modifying the input Data for workRequestC.

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

Protected methods

internalInit

protected void internalInit (Context appContext, 
                UUID id, 
                Extras extras)

Parameters
appContext Context

id UUID

extras Extras