Worker
abstract class Worker : ListenableWorker
WorkManagerScheduler.SchedulerWorker |
A |
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.executor). 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 |
|---|
Worker(context: Context, workerParams: WorkerParameters) |
Public functions |
|
|---|---|
abstract ListenableWorker.Result |
Override this method to do your actual background processing. |
open ForegroundInfo |
An instance of |
open ListenableFuture<ForegroundInfo> |
Return an instance of |
final ListenableFuture<ListenableWorker.Result> |
Override this method to start your actual background processing. |
Inherited functions |
||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public functions
doWork
@WorkerThread
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 has a well defined execution window to finish its execution and return a androidx.work.ListenableWorker.Result. After this time has expired, the Worker will be signalled to stop.
| Returns | |
|---|---|
ListenableWorker.Result |
The |
getForegroundInfo
@WorkerThread
open fun getForegroundInfo(): ForegroundInfo
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.
| Returns | |
|---|---|
ForegroundInfo |
A |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if it is not overridden and worker tries to go to foreground |
getForegroundInfoAsync
open fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo>
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.
| Returns | |
|---|---|
ListenableFuture<ForegroundInfo> |
A |
startWork
final fun startWork(): ListenableFuture<ListenableWorker.Result>
Override this method to start your actual background processing. This method is called on the main thread.
A ListenableWorker has a well defined execution window to to finish its execution and return a Result. After this time has expired, the worker will be signalled to stop and its com.google.common.util.concurrent.ListenableFuture will be cancelled.
The future will also be cancelled if this worker is stopped for any reason (see onStopped).
| Returns | |
|---|---|
ListenableFuture<ListenableWorker.Result> |
A |