WorkInfo
public final class WorkInfo
Information about a particular WorkRequest containing the id of the WorkRequest, its current State, output, tags, and run attempt count. Note that output is only available for the terminal states (State.SUCCEEDED and State.FAILED).
Summary
Nested types |
|---|
public final class WorkInfo.PeriodicityInfoA periodic work's interval and flex duration |
public enum WorkInfo.State extends EnumThe current lifecycle state of a |
Constants |
|
|---|---|
static final int |
The current standby bucket requires that the job stop now. |
static final int |
The app is restricted from running in the background. |
static final int |
The worker was cancelled directly by the app, either by calling cancel methods, e.g. |
static final int |
The requested battery-not-low constraint is no longer satisfied. |
static final int |
The requested charging constraint is no longer satisfied. |
static final int |
The requested connectivity constraint is no longer satisfied. |
static final int |
The requested idle constraint is no longer satisfied. |
static final int |
The requested storage-not-low constraint is no longer satisfied. |
static final int |
The device state (eg. |
static final int |
The system's estimate of when the app will be launched changed significantly enough to decide this worker shouldn't be running right now. |
static final int |
The foreground worker used up its maximum execution time and timed out. |
static final int |
Additional stop reason, that is returned from |
static final int |
The job was stopped to run a higher priority job of the app. |
static final int |
STOP_REASON_QUOTA = 10The app has consumed all of its current quota. |
static final int |
The system is doing some processing that requires stopping this job. |
static final int |
The worker used up its maximum execution time and timed out. |
static final int |
Stop reason that is used in cases when worker did stop, but the reason for this is unknown. |
static final int |
STOP_REASON_USER = 13The user stopped the job. |
Public constructors |
|---|
WorkInfo( |
Public methods |
|
|---|---|
boolean |
|
final @NonNull Constraints |
|
final int |
The latest generation of this Worker. |
final @NonNull UUID |
getId()The identifier of the |
final long |
The initial delay for this work set in the |
final long |
The earliest time this work is eligible to run next, if this work is |
final @NonNull Data |
The output |
final WorkInfo.PeriodicityInfo |
For periodic work, the period and flex duration set in the |
final @NonNull Data |
The progress |
final @IntRange(from = 0) int |
The run attempt count of the |
final @NonNull WorkInfo.State |
getState()The current |
final int |
@RequiresApi(value = 31)The reason why this worker was stopped on the previous run attempt. |
final @NonNull Set<@NonNull String> |
getTags()The |
final String |
The worker class specified when the work request was enqueued. |
int |
hashCode() |
@NonNull String |
toString() |
Constants
STOP_REASON_APP_STANDBY
public static final int STOP_REASON_APP_STANDBY = 12
The current standby bucket requires that the job stop now.
| See also | |
|---|---|
STOP_REASON_APP_STANDBY |
STOP_REASON_BACKGROUND_RESTRICTION
public static final int STOP_REASON_BACKGROUND_RESTRICTION = 11
The app is restricted from running in the background.
| See also | |
|---|---|
STOP_REASON_BACKGROUND_RESTRICTION |
STOP_REASON_CANCELLED_BY_APP
public static final int STOP_REASON_CANCELLED_BY_APP = 1
The worker was cancelled directly by the app, either by calling cancel methods, e.g. WorkManager.cancelUniqueWork, or enqueueing uniquely named worker with a policy that cancels an existing worker, e.g. ExistingWorkPolicy.REPLACE.
STOP_REASON_CONSTRAINT_BATTERY_NOT_LOW
public static final int STOP_REASON_CONSTRAINT_BATTERY_NOT_LOW = 5
The requested battery-not-low constraint is no longer satisfied.
| See also | |
|---|---|
setRequiresBatteryNotLow |
STOP_REASON_CONSTRAINT_CHARGING
public static final int STOP_REASON_CONSTRAINT_CHARGING = 6
The requested charging constraint is no longer satisfied.
| See also | |
|---|---|
setRequiresCharging |
STOP_REASON_CONSTRAINT_CONNECTIVITY
public static final int STOP_REASON_CONSTRAINT_CONNECTIVITY = 7
The requested connectivity constraint is no longer satisfied.
STOP_REASON_CONSTRAINT_DEVICE_IDLE
public static final int STOP_REASON_CONSTRAINT_DEVICE_IDLE = 8
The requested idle constraint is no longer satisfied.
STOP_REASON_CONSTRAINT_STORAGE_NOT_LOW
public static final int STOP_REASON_CONSTRAINT_STORAGE_NOT_LOW = 9
The requested storage-not-low constraint is no longer satisfied.
STOP_REASON_DEVICE_STATE
public static final int STOP_REASON_DEVICE_STATE = 4
The device state (eg. Doze, battery saver, memory usage, etc) requires WorkManager to stop this worker.
STOP_REASON_ESTIMATED_APP_LAUNCH_TIME_CHANGED
public static final int STOP_REASON_ESTIMATED_APP_LAUNCH_TIME_CHANGED = 15
The system's estimate of when the app will be launched changed significantly enough to decide this worker shouldn't be running right now.
STOP_REASON_FOREGROUND_SERVICE_TIMEOUT
public static final int STOP_REASON_FOREGROUND_SERVICE_TIMEOUT
The foreground worker used up its maximum execution time and timed out.
Foreground workers have a maximum execution time limit depending on the ForegroundInfo type. See the notes on android.content.pm.ServiceInfo types.
STOP_REASON_NOT_STOPPED
public static final int STOP_REASON_NOT_STOPPED
Additional stop reason, that is returned from WorkInfo.stopReason in cases when a worker in question wasn't stopped. E.g. when a worker was just enqueued, but didn't run yet.
STOP_REASON_PREEMPT
public static final int STOP_REASON_PREEMPT = 2
The job was stopped to run a higher priority job of the app.
STOP_REASON_QUOTA
public static final int STOP_REASON_QUOTA = 10
The app has consumed all of its current quota. Each app is assigned a quota of how much it can run workers within a certain time frame. The quota is informed, in part, by app standby buckets.
| See also | |
|---|---|
STOP_REASON_QUOTA |
STOP_REASON_SYSTEM_PROCESSING
public static final int STOP_REASON_SYSTEM_PROCESSING = 14
The system is doing some processing that requires stopping this job.
| See also | |
|---|---|
STOP_REASON_SYSTEM_PROCESSING |
STOP_REASON_TIMEOUT
public static final int STOP_REASON_TIMEOUT = 3
The worker used up its maximum execution time and timed out. Each individual worker has a maximum execution time limit, regardless of how much total quota the app has. See the note on JobScheduler for the execution time limits.
STOP_REASON_UNKNOWN
public static final int STOP_REASON_UNKNOWN
Stop reason that is used in cases when worker did stop, but the reason for this is unknown. For example, when the app abruptly stopped due to a crash or when a device suddenly ran out of the battery.
STOP_REASON_USER
public static final int STOP_REASON_USER = 13
The user stopped the job. This can happen either through force-stop, adb shell commands, uninstalling, or some other UI.
| See also | |
|---|---|
STOP_REASON_USER |
Public constructors
WorkInfo
public WorkInfo(
@NonNull UUID id,
@NonNull WorkInfo.State state,
@NonNull Set<@NonNull String> tags,
@NonNull Data outputData,
@NonNull Data progress,
int runAttemptCount,
int generation,
@NonNull Constraints constraints,
long initialDelayMillis,
WorkInfo.PeriodicityInfo periodicityInfo,
long nextScheduleTimeMillis,
int stopReason,
String workerClassName
)
Public methods
getConstraints
public final @NonNull Constraints getConstraints()
Constraints of this worker.
getGeneration
public final int getGeneration()
The latest generation of this Worker.
A work has multiple generations, if it was updated via WorkManager.updateWork or WorkManager.enqueueUniquePeriodicWork using ExistingPeriodicWorkPolicy.UPDATE.
If this worker is currently running, it can possibly be of an older generation rather than returned by this function if an update has happened during an execution of this worker.
getInitialDelayMillis
public final long getInitialDelayMillis()
The initial delay for this work set in the WorkRequest
getNextScheduleTimeMillis
public final long getNextScheduleTimeMillis()
The earliest time this work is eligible to run next, if this work is State.ENQUEUED.
This is the earliest System.currentTimeMillis time that WorkManager would consider running this work, regardless of any other system. It only represents the time that the initialDelay, periodic configuration, and backoff criteria are considered to be met.
Work will almost never run at this time in the real world. This method is intended for use in scheduling tests or to check set schedules in app. Work run times are dependent on many factors like the underlying system scheduler, doze and power saving modes of the OS, and meeting any configured constraints. This is expected and is not considered a bug.
The returned value may be in the past if the work was not able to run at that time. It will be eligible to run any time after that time.
Defaults to Long.MAX_VALUE for all other states, including if the work is currently State.RUNNING or State.BLOCKED on prerequisite work.
Even if this value is set, the work may not be registered with the system scheduler if there are limited scheduling slots or other factors.
getOutputData
public final @NonNull Data getOutputData()
The output Data for the WorkRequest. If the WorkRequest is unfinished, this is always Data.EMPTY.
getPeriodicityInfo
public final WorkInfo.PeriodicityInfo getPeriodicityInfo()
For periodic work, the period and flex duration set in the PeriodicWorkRequest.
Null if this is onetime work.
getProgress
public final @NonNull Data getProgress()
The progress Data associated with the WorkRequest.
getRunAttemptCount
public final @IntRange(from = 0) int getRunAttemptCount()
The run attempt count of the WorkRequest. Note that for PeriodicWorkRequests, the run attempt count gets reset between successful runs.
getState
public final @NonNull WorkInfo.State getState()
The current State of the WorkRequest.
getStopReason
@RequiresApi(value = 31)
public final int getStopReason()
The reason why this worker was stopped on the previous run attempt.
For a worker being stopped, at first it should have attempted to run, i.e. its state should be == RUNNING and then ListenableWorker.onStopped should have been called, resulting in this worker's state going back WorkInfo.State.ENQUEUED. In this situation (runAttemptCount > 0 and state == ENQUEUED) this stopReason property could be checked to see for additional information. Please note, that this state (runAttemptCount > 0 and state == ENQUEUED) can happen not only because a worker was stopped, but also when a worker returns ListenableWorker.Result.retry(). In this situation this property will return STOP_REASON_NOT_STOPPED.
getTags
public final @NonNull Set<@NonNull String> getTags()
The Set of tags associated with the WorkRequest.
getWorkerClassName
public final String getWorkerClassName()
The worker class specified when the work request was enqueued.