Worker
abstract class Worker : ListenableWorker
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
Inherited functions |
From class ListenableWorker
Context |
getApplicationContext()
Gets the application android.content.Context .
|
ListenableFuture<ForegroundInfo!> |
getForegroundInfoAsync()
Return an instance of ForegroundInfo if the WorkRequest is important to the user. In this case, WorkManager provides a signal to the OS that the process should be kept alive while this work is executing.
Prior to Android S, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, showing the notification provided in the ForegroundInfo . To update this notification subsequently, the application can use android.app.NotificationManager .
Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.
|
UUID |
getId()
Gets the ID of the WorkRequest that created this Worker.
|
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 .
|
Network? |
getNetwork()
Gets the android.net.Network to use for this Worker. This method returns null if there is no network needed for this work request.
|
Int |
getRunAttemptCount()
Gets the current run attempt count for this work. Note that for periodic work, this value gets reset between periods.
|
MutableSet<String!> |
getTags()
Gets a java.util.Set of tags associated with this Worker's WorkRequest .
|
MutableList<String!> |
getTriggeredContentAuthorities()
Gets the list of content authorities that caused this Worker to execute. See JobParameters#getTriggeredContentAuthorities() for relevant JobScheduler code.
|
MutableList<Uri!> |
getTriggeredContentUris()
Gets the list of content android.net.Uri s that caused this Worker to execute. See JobParameters#getTriggeredContentUris() for relevant JobScheduler code.
|
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. WorkManager will retry the work at a later time if necessary.
|
Unit |
onStopped()
This method is invoked when this Worker has been told to stop. At this point, the ListenableFuture returned by the instance of startWork() is also cancelled. 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.
|
ListenableFuture<Void!> |
setForegroundAsync(@NonNull foregroundInfo: ForegroundInfo)
This specifies that the WorkRequest is long-running or otherwise important. In this case, WorkManager provides a signal to the OS that the process should be kept alive if possible while this work is executing.
Calls to setForegroundAsync *must* complete before a ListenableWorker signals completion by returning a Result .
Under the hood, WorkManager manages and runs a foreground service on your behalf to execute this WorkRequest, showing the notification provided in ForegroundInfo .
|
ListenableFuture<Void!> |
setProgressAsync(@NonNull data: Data)
Updates ListenableWorker progress.
|
|
Public constructors
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.