Worker
public abstract class Worker extends 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(@NonNull Context context, @NonNull WorkerParameters workerParams) |
Public methods |
|
---|---|
abstract @NonNull ListenableWorker.Result |
Override this method to do your actual background processing. |
@NonNull ForegroundInfo |
An instance of |
@NonNull ListenableFuture<@NonNull ForegroundInfo> |
Return an instance of |
final @NonNull ListenableFuture<@NonNull ListenableWorker.Result> |
Override this method to start your actual background processing. |
Inherited methods |
||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
doWork
@WorkerThread
public abstract @NonNull ListenableWorker.Result doWork()
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 | |
---|---|
@NonNull ListenableWorker.Result |
The |
getForegroundInfo
@WorkerThread
public @NonNull ForegroundInfo getForegroundInfo()
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 | |
---|---|
@NonNull ForegroundInfo |
A |
Throws | |
---|---|
kotlin.IllegalStateException |
if it is not overridden and worker tries to go to foreground |
getForegroundInfoAsync
public @NonNull ListenableFuture<@NonNull 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.
Returns | |
---|---|
@NonNull ListenableFuture<@NonNull ForegroundInfo> |
A |
startWork
public final @NonNull ListenableFuture<@NonNull ListenableWorker.Result> startWork()
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 | |
---|---|
@NonNull ListenableFuture<@NonNull ListenableWorker.Result> |
A |