CoroutineWorker
public abstract class CoroutineWorker extends ListenableWorker
A ListenableWorker implementation that provides interop with Kotlin Coroutines. Override the doWork function to do your suspending work.
By default, CoroutineWorker runs on Dispatchers.Default if neither Configuration.Builder.setExecutor or Configuration.Builder.setWorkerCoroutineContext were set.
A CoroutineWorker is given a maximum of ten minutes to finish its execution and return a [ListenableWorker.Result]. After this time has expired, the worker will be signalled to stop.
Summary
Public constructors |
|---|
CoroutineWorker( |
Public methods |
|
|---|---|
abstract @NonNull ListenableWorker.Result |
doWork()A suspending method to do your work. |
@NonNull CoroutineDispatcher |
This method is deprecated. use withContext(...) inside doWork() instead. |
@NonNull ForegroundInfo |
|
final @NonNull ListenableFuture<@NonNull ForegroundInfo> |
Return an instance of |
final void |
This method is invoked when this Worker has been told to stop. |
final void |
setForeground(@NonNull ForegroundInfo foregroundInfo)Makes the |
final void |
setProgress(@NonNull Data data)Updates the progress for the |
final @NonNull ListenableFuture<@NonNull ListenableWorker.Result> |
Override this method to start your actual background processing. |
Inherited methods |
||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
CoroutineWorker
public CoroutineWorker(
@NonNull Context appContext,
@NonNull WorkerParameters params
)
Public methods
doWork
public abstract @NonNull ListenableWorker.Result doWork()
A suspending method to do your work.
To specify which [CoroutineDispatcher] your work should run on, use `withContext()` within `doWork()`. If there is no other dispatcher declared, [Dispatchers.Default] will be used.
A CoroutineWorker is given a maximum of ten minutes to finish its execution and return a [ListenableWorker.Result]. After this time has expired, the worker will be signalled to stop.
| Returns | |
|---|---|
@NonNull ListenableWorker.Result |
The |
public @NonNull CoroutineDispatchergetCoroutineContext()
The coroutine context on which doWork will run.
If this property is overridden then it takes precedent over Configuration.executor or Configuration.workerCoroutineContext.
By default, this is a dispatcher delegating to Dispatchers.Default
getForegroundInfo
public @NonNull ForegroundInfo getForegroundInfo()
| Returns | |
|---|---|
@NonNull ForegroundInfo |
The |
| Throws | |
|---|---|
kotlin.IllegalStateException |
when not overridden. Override this method when the corresponding |
getForegroundInfoAsync
public final @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 |
onStopped
public final void onStopped()
This method is invoked when this Worker has been told to stop. At this point, the com.google.common.util.concurrent.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.
setForeground
public final void setForeground(@NonNull ForegroundInfo foregroundInfo)
Makes the CoroutineWorker run in the context of a foreground android.app.Service. This is a suspending function unlike the setForegroundAsync API which returns a ListenableFuture.
Calling setForeground will throw an IllegalStateException if the process is subject to foreground service restrictions. Consider using WorkRequest.Builder.setExpedited and getForegroundInfo instead.
| Parameters | |
|---|---|
@NonNull ForegroundInfo foregroundInfo |
The |
setProgress
public final void setProgress(@NonNull Data data)
Updates the progress for the CoroutineWorker. This is a suspending function unlike the setProgressAsync API which returns a ListenableFuture.
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 |