package

androidx.work

Overview

WorkManager the recommended library for persistent work. Scheduled work is guaranteed to execute sometime after its 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 ListenableWorker class. A simple implementation, 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: OneTimeWorkRequest and 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 Constraints, inputs (see Data), and backoff criteria. WorkRequests can be tagged with human-readable Strings (see WorkRequest.Builder.addTag(String)), 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 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 Configuration by using Configuration.Provider or WorkManager.

WorkManager and its Interactions with the OS

WorkManager BroadcastReceivers to monitor Constraints on devices before API 23. The BroadcastReceivers are disabled on API 23 and up. In particular, WorkManager listens to the following Intents:

  • 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
In addition, WorkManager listens to system time changes and reboots to properly reschedule work in certain situations. For this, it listens to the following Intents:

  • 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 23
  • android.permission.ACCESS_NETWORK_STATE to listen to network changes before API 23 and monitor network 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).

Interfaces

Configuration.ProviderA class that can provide the Configuration for WorkManager and allow for on-demand initialization of WorkManager.
ForegroundUpdaterManages updating android.app.Notifications when a ListenableWorker transitions to running in the context of a foreground .
InitializationExceptionHandlerAn Exception Handler that can be used to determine if there were issues when trying to initialize WorkManager.
OperationAn object that provides information about the execution of an asynchronous command being performed by WorkManager.
ProgressUpdaterUpdates progress for a ListenableWorker.
RunnableSchedulerCan be used to schedule java.lang.Runnables after a delay in milliseconds.

Classes

ArrayCreatingInputMergerAn InputMerger that attempts to merge the inputs, creating arrays when necessary.
ConfigurationThe Configuration object used to customize WorkManager upon initialization.
Configuration.BuilderA Builder for Configurations.
ConstraintsA specification of the requirements that need to be met before a WorkRequest can run.
Constraints.BuilderA Builder for a Constraints object.
ContentUriTriggersA container for ContentUriTriggers.Triggers that caused a worker's Constraints to be met.
ContentUriTriggers.TriggerDefines a content trigger for a WorkRequest.
DataA persistable set of key/value pairs which are used as inputs and outputs for ListenableWorkers.
Data.BuilderA builder for Data objects.
DelegatingWorkerFactoryA WorkerFactory which delegates to other factories.
ForegroundInfoThe information required when a ListenableWorker runs in the context of a foreground service.
InputMergerAn abstract class that allows the user to define how to merge a list of inputs to a ListenableWorker.
InputMergerFactoryA factory object that creates InputMerger instances.
ListenableWorkerA class that can perform work asynchronously in WorkManager.
ListenableWorker.ResultThe result of a ListenableWorker's computation.
ListenableWorker.Result.FailureUsed to indicate that the work completed with a permanent failure.
ListenableWorker.Result.RetryUsed to indicate that the work encountered a transient failure and should be retried with backoff specified in WorkRequest.Builder.setBackoffCriteria(BackoffPolicy, long, TimeUnit).
ListenableWorker.Result.SuccessUsed to indicate that the work completed successfully.
LoggerThe class that handles logging requests for WorkManager.
Logger.LogcatLoggerThe default Logger implementation that writes to logcat when the requests meet or exceed the loggingLevel specified in the constructor.
OneTimeWorkRequestA WorkRequest for non-repeating work.
OneTimeWorkRequest.BuilderBuilder for OneTimeWorkRequests.
Operation.StateThe lifecycle state of an Operation.
Operation.State.FAILUREThis represents an Operation which has failed.
Operation.State.IN_PROGRESSThis represents an Operation which is in progress.
Operation.State.SUCCESSThis represents an Operation which is successful.
OverwritingInputMergerAn InputMerger that attempts to add all keys from all inputs to the output.
PeriodicWorkRequestA WorkRequest for repeating work.
PeriodicWorkRequest.BuilderBuilder for PeriodicWorkRequests.
RxWorkerRxJava2 interoperability Worker implementation.
WorkContinuationA class that allows you to chain together OneTimeWorkRequests.
WorkerA class that performs work synchronously on a background thread provided by WorkManager.
WorkerFactoryA factory object that creates ListenableWorker instances.
WorkerParametersSetup parameters for a ListenableWorker.
WorkerParameters.RuntimeExtrasExtra runtime information for Workers.
WorkInfoInformation about a particular WorkRequest containing the id of the WorkRequest, its current WorkInfo.State, output, tags, and run attempt count.
WorkManagerWorkManager the recommended library for persistent work.
WorkManagerInitializerInitializes WorkManager using androidx.startup.
WorkQueryA specification for querying WorkRequests.
WorkQuery.BuilderA builder for WorkQuery.
WorkRequestThe base class for specifying parameters for work that should be enqueued in WorkManager.
WorkRequest.Builder<B, W>A builder for WorkRequests.

Enums

BackoffPolicyAn enumeration of backoff policies when retrying work.
ExistingPeriodicWorkPolicyAn enumeration of the conflict resolution policies available to unique PeriodicWorkRequests in case of a collision.
ExistingWorkPolicyAn enumeration of the conflict resolution policies available to unique OneTimeWorkRequests in case of a collision.
NetworkTypeAn enumeration of various network types that can be used as Constraints for work.
OutOfQuotaPolicyAn enumeration of policies that help determine out of quota behavior for expedited jobs.
WorkInfo.StateThe current lifecycle state of a WorkRequest.