androidx.work
WorkManager the recommended library for persistent work. Scheduled work is guaranteed to execute sometime after its androidx.work.Constraints
are met. WorkManager allows observation of work status and the ability to create complex chains of work.
WorkManager uses an underlying job dispatching service when available based on the following criteria:
- Uses JobScheduler for API 23+
- Uses a custom AlarmManager + BroadcastReceiver implementation for API 14-22
All work must be done in a androidx.work.ListenableWorker
class. A simple implementation, androidx.work.Worker
, is recommended as the starting point for most developers. With the optional dependencies, you can also use CoroutineWorker
or RxWorker
. All background work is given a maximum of ten minutes to finish its execution. After this time has expired, the worker will be signalled to stop.
There are two types of work supported by WorkManager: androidx.work.OneTimeWorkRequest
and androidx.work.PeriodicWorkRequest
. OneTimeWorkRequests can be chained together into acyclic graphs. Work is eligible for execution when all of its prerequisites are complete. If any of its prerequisites fail or are cancelled, the work will never run.
WorkRequests can accept androidx.work.Constraints
, inputs (see androidx.work.Data
), and backoff criteria. WorkRequests can be tagged with human-readable Strings (see addTag
), and chains of work can be given a uniquely-identifiable name with conflict policies. *
Initializing WorkManager
By default, WorkManager is initialized using a ContentProvider
with a default androidx.work.Configuration
. ContentProviders are created and run before the Application
object, so this allows the WorkManager singleton to be setup before your code can run in most cases. This is suitable for most developers. However, you can provide a custom androidx.work.Configuration
by using androidx.work.Configuration.Provider
or initialize
.
WorkManager and its Interactions with the OS
WorkManager uses BroadcastReceiver
s to monitor androidx.work.Constraints
on devices before API 23. The BroadcastReceivers are disabled on API 23 and up. In particular, WorkManager listens to the following Intent
s:
android.intent.action.ACTION_POWER_CONNECTED
android.intent.action.ACTION_POWER_DISCONNECTED
android.intent.action.BATTERY_OKAY
android.intent.action.BATTERY_LOW
android.intent.action.DEVICE_STORAGE_LOW
android.intent.action.DEVICE_STORAGE_OK
android.net.conn.CONNECTIVITY_CHANGE
android.intent.action.BOOT_COMPLETED
android.intent.action.TIME_SET
android.intent.action.TIMEZONE_CHANGED
WorkManager uses the following permissions:
android.permission.WAKE_LOCK
to make it can keep the device awake to complete work before API 23android.permission.ACCESS_NETWORK_STATE
to listen to network changes before API 23 and monitor networkandroidx.work.Constraints
android.permission.RECEIVE_BOOT_COMPLETED
to listen to reboots and reschedule work properly.
Note that WorkManager may enable or disable some of its BroadcastReceivers at runtime as needed. This has the side-effect of the system sending ACTION_PACKAGE_CHANGED
broadcasts to your app. Please be aware of this use case and architect your app appropriately (especially if you are using widgets - see https://issuetracker.google.com/115575872).
Annotations
ExperimentalConfigurationApi |
Annotation indicating experimental API for new WorkManager's Configuration APIs. |
Interfaces
Clock |
The interface WorkManager uses to access to the current time. |
Configuration.Provider |
A class that can provide the |
ForegroundUpdater |
Manages updating |
Operation |
An object that provides information about the execution of an asynchronous command being performed by |
ProgressUpdater |
Updates progress for a |
RunnableScheduler |
Can be used to schedule |
Classes
ArrayCreatingInputMerger |
An |
Configuration |
The Configuration object used to customize |
Configuration.Builder |
A Builder for |
Constraints |
A specification of the requirements that need to be met before a |
Constraints.Builder |
A Builder for a |
Constraints.ContentUriTrigger |
This class describes a content uri trigger on the |
CoroutineWorker |
A |
Data |
A persistable set of key/value pairs which are used as inputs and outputs for |
Data.Builder |
A builder for |
DataKt |
|
DelegatingWorkerFactory |
A |
ForegroundInfo |
The information required when a |
InputMerger |
An abstract class that allows the user to define how to merge a list of inputs to a |
InputMergerFactory |
A factory object that creates |
ListenableWorker |
A class that can perform work asynchronously in |
ListenableWorker.Result |
The result of a |
OneTimeWorkRequest |
A |
OneTimeWorkRequest.Builder |
Builder for |
OneTimeWorkRequestKt |
|
Operation.State |
The lifecycle state of an |
Operation.State.FAILURE |
This represents an |
Operation.State.IN_PROGRESS |
This represents an |
Operation.State.SUCCESS |
This represents an |
OperationKt |
|
OverwritingInputMerger |
An |
PeriodicWorkRequest |
A |
PeriodicWorkRequest.Builder |
Builder for |
PeriodicWorkRequestKt |
|
RxWorker |
RxJava2 interoperability Worker implementation. |
WorkContinuation |
A class that allows you to chain together |
WorkInfo |
Information about a particular |
WorkInfo.PeriodicityInfo |
A periodic work's interval and flex duration |
WorkManager |
WorkManager is the recommended library for persistent work. |
WorkManagerInitializer |
Initializes |
WorkQuery |
A specification for querying |
WorkQuery.Builder |
A builder for |
WorkRequest |
The base class for specifying parameters for work that should be enqueued in |
WorkRequest.Builder |
A builder for |
Worker |
A class that performs work synchronously on a background thread provided by |
WorkerExceptionInfo |
Represents exceptions occurred from initializing or executing a |
WorkerFactory |
A factory object that creates |
WorkerParameters |
Setup parameters for a |
WorkerParametersExtensions |
Enums
BackoffPolicy |
An enumeration of backoff policies when retrying work. |
ExistingPeriodicWorkPolicy |
An enumeration of the conflict resolution policies available to unique |
ExistingWorkPolicy |
An enumeration of the conflict resolution policies available to unique |
NetworkType |
An enumeration of various network types that can be used as |
OutOfQuotaPolicy |
An enumeration of policies that help determine out of quota behavior for expedited jobs. |
WorkInfo.State |
The current lifecycle state of a |
WorkManager.UpdateResult |
An enumeration of results for |