ListenableWorker
abstract class ListenableWorker
kotlin.Any | |
↳ | androidx.work.ListenableWorker |
A class that can perform work asynchronously in WorkManager
. For most cases, we recommend using Worker
, which offers a simple synchronous API that is executed on a pre-specified background thread.
ListenableWorker classes are instantiated at runtime by the WorkerFactory
specified in the Configuration
. The startWork()
method is called on the main thread.
In case the work is preempted and later restarted for any reason, a new instance of ListenableWorker is created. This means that startWork
is called exactly once per ListenableWorker instance. A new ListenableWorker is created if a unit of work needs to be rerun.
A ListenableWorker is given a maximum of ten minutes to finish its execution and return a Result
. After this time has expired, the worker will be signalled to stop and its ListenableFuture
will be cancelled.
Exercise caution when renaming or removing ListenableWorkers from your codebase.
Summary
Nested classes | |
---|---|
abstract |
The result of a |
Public constructors | |
---|---|
<init>(@NonNull appContext: Context, @NonNull workerParams: WorkerParameters) |
Public methods | |
---|---|
Context |
Gets the application |
open ListenableFuture<ForegroundInfo!> |
Return an instance of |
UUID |
getId() Gets the ID of the |
Data |
Gets the input data. |
Network? |
Gets the |
Int |
Gets the current run attempt count for this work. |
MutableSet<String!> |
getTags() Gets a |
MutableList<String!> |
Gets the list of content authorities that caused this Worker to execute. |
MutableList<Uri!> |
Gets the list of content |
Boolean |
Returns |
open Unit |
This method is invoked when this Worker has been told to stop. |
ListenableFuture<Void!> |
setForegroundAsync(@NonNull foregroundInfo: ForegroundInfo) This specifies that the |
ListenableFuture<Void!> |
setProgressAsync(@NonNull data: Data) Updates |
abstract ListenableFuture<ListenableWorker.Result!> |
Override this method to start your actual background processing. |
Public constructors
<init>
ListenableWorker(
@NonNull appContext: Context,
@NonNull workerParams: WorkerParameters)
Parameters | |
---|---|
appContext |
Context: The application Context |
workerParams |
WorkerParameters: Parameters to setup the internal state of this worker |
Public methods
getApplicationContext
@NonNull fun getApplicationContext(): Context
Gets the application android.content.Context
.
Return | |
---|---|
Context |
The application android.content.Context |
getForegroundInfoAsync
@NonNull 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.
Return | |
---|---|
ListenableFuture<ForegroundInfo!> |
A ListenableFuture of ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at WorkRequest.B |