[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

JobScheduler  |  Android Developers

public abstract class JobScheduler
extends Object



This is an API for scheduling various types of jobs against the framework that will be executed in your application's own process.

See JobInfo for more description of the types of jobs that can be run and how to construct them. You will construct these JobInfo objects and pass them to the JobScheduler with schedule(android.app.job.JobInfo). When the criteria declared are met, the system will execute this job on your application's JobService. You identify the service component that implements the logic for your job when you construct the JobInfo using JobInfo.Builder.Builder(int, android.content.ComponentName).

The framework will be intelligent about when it executes jobs, and attempt to batch and defer them as much as possible. Typically, if you don't specify a deadline on a job, it can be run at any moment depending on the current state of the JobScheduler's internal queue.

Starting in Android version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, JobScheduler may try to optimize job execution by shifting execution to times with more available system resources in order to lower user impact. Factors in system health include sufficient battery, idle, charging, and access to an un-metered network. Jobs will initially be treated as if they have all these requirements, but as their deadlines approach, restrictions will become less strict. Requested requirements will not be affected by this change.

While a job is running, the system holds a wakelock on behalf of your app. For this reason, you do not need to take any action to guarantee that the device stays awake for the duration of the job.

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.JOB_SCHEDULER_SERVICE).

Prior to Android version Build.VERSION_CODES.S, jobs could only have a maximum of 100 jobs scheduled at a time. Starting with Android version Build.VERSION_CODES.S, that limit has been increased to 150. Expedited jobs also count towards the limit.

In Android version Build.VERSION_CODES.LOLLIPOP, jobs had a maximum execution time of one minute. Starting with Android version Build.VERSION_CODES.M and ending with Android version Build.VERSION_CODES.R, jobs had a maximum execution time of 10 minutes. Starting from Android version Build.VERSION_CODES.S, jobs will still be stopped after 10 minutes if the system is busy or needs the resources, but if not, jobs may continue running longer than 10 minutes.

Note: Beginning with API 30 (Build.VERSION_CODES.R), JobScheduler will throttle runaway applications. Calling schedule(android.app.job.JobInfo) and other such methods with very high frequency can have a high cost and so, to make sure the system doesn't get overwhelmed, JobScheduler will begin to throttle apps, regardless of target SDK version.

See also:

Summary

Constants

int PENDING_JOB_REASON_APP

The app is in a state that prevents the job from running (eg.

int PENDING_JOB_REASON_APP_STANDBY

The current standby bucket prevents the job from running.

int PENDING_JOB_REASON_BACKGROUND_RESTRICTION

The app is restricted from running in the background.

int PENDING_JOB_REASON_CONSTRAINT_BATTERY_NOT_LOW

The requested battery-not-low constraint is not satisfied.

int PENDING_JOB_REASON_CONSTRAINT_CHARGING

The requested charging constraint is not satisfied.

int PENDING_JOB_REASON_CONSTRAINT_CONNECTIVITY

The requested connectivity constraint is not satisfied.

int PENDING_JOB_REASON_CONSTRAINT_CONTENT_TRIGGER

The requested content trigger constraint is not satisfied.

int PENDING_JOB_REASON_CONSTRAINT_DEVICE_IDLE

The requested idle constraint is not satisfied.

int PENDING_JOB_REASON_CONSTRAINT_MINIMUM_LATENCY

The minimum latency has not transpired.

int PENDING_JOB_REASON_CONSTRAINT_PREFETCH

The system's estimate of when the app will be launched is far away enough to warrant delaying this job.

int PENDING_JOB_REASON_CONSTRAINT_STORAGE_NOT_LOW

The requested storage-not-low constraint is not satisfied.

int PENDING_JOB_REASON_DEVICE_STATE

The job is being deferred due to the device state (eg.

int PENDING_JOB_REASON_EXECUTING

The job is currently running and is therefore not pending.

int PENDING_JOB_REASON_INVALID_JOB_ID

The job doesn't exist.

int PENDING_JOB_REASON_JOB_SCHEDULER_OPTIMIZATION

JobScheduler thinks it can defer this job to a more optimal running time.

int PENDING_JOB_REASON_QUOTA

The app has consumed all of its current quota.

int PENDING_JOB_REASON_UNDEFINED

There is no known reason why the job is pending.

int PENDING_JOB_REASON_USER

JobScheduler is respecting one of the user's actions (eg.

int RESULT_FAILURE

Returned from schedule(android.app.job.JobInfo) if a job wasn't scheduled successfully.

int RESULT_SUCCESS

Returned from schedule(android.app.job.JobInfo) if this job has been successfully scheduled.

Public constructors

JobScheduler()

Public methods

boolean canRunLongJobs()

Returns true if the calling app currently holds the Manifest.permission.RUN_LONG_JOBS permission, allowing it to run long jobs.

abstract void cancel(int jobId)

Cancel the specified job.

abstract void cancelAll()

Cancel all jobs that have been scheduled by the calling application.

abstract int enqueue(JobInfo job, JobWorkItem work)

Similar to schedule(JobInfo), but allows you to enqueue work for a new or existing job.

abstract List<JobInfo> getAllPendingJobs()

Retrieve all jobs that have been scheduled by the calling application.

abstract JobInfo getPendingJob(int jobId)

Look up the description of a scheduled job.

int getPendingJobReason(int jobId)

Returns a reason why the job is pending and not currently executing.

abstract int schedule(JobInfo job)

Schedule a job to be executed.

Inherited methods

From class java.lang.Object

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Constants

PENDING_JOB_REASON_APP

public static final int PENDING_JOB_REASON_APP

The app is in a state that prevents the job from running (eg. the JobService component is disabled).

Constant Value: 1 (0x00000001)

PENDING_JOB_REASON_APP_STANDBY

public static final int PENDING_JOB_REASON_APP_STANDBY

The current standby bucket prevents the job from running.

Constant Value: 2 (0x00000002)

PENDING_JOB_REASON_BACKGROUND_RESTRICTION

public static final int PENDING_JOB_REASON_BACKGROUND_RESTRICTION

The app is restricted from running in the background.

Constant Value: 3 (0x00000003)

PENDING_JOB_REASON_CONSTRAINT_BATTERY_NOT_LOW

public static final int PENDING_JOB_REASON_CONSTRAINT_BATTERY_NOT_LOW

The requested battery-not-low constraint is not satisfied.

Constant Value: 4 (0x00000004)

PENDING_JOB_REASON_CONSTRAINT_CHARGING

public static final int PENDING_JOB_REASON_CONSTRAINT_CHARGING

The requested charging constraint is not satisfied.

Constant Value: 5 (0x00000005)

PENDING_JOB_REASON_CONSTRAINT_CONTENT_TRIGGER

public static final int PENDING_JOB_REASON_CONSTRAINT_CONTENT_TRIGGER

The requested content trigger constraint is not satisfied.

Constant Value: 7 (0x00000007)

PENDING_JOB_REASON_CONSTRAINT_DEVICE_IDLE

public static final int PENDING_JOB_REASON_CONSTRAINT_DEVICE_IDLE

The requested idle constraint is not satisfied.

Constant Value: 8 (0x00000008)

PENDING_JOB_REASON_CONSTRAINT_MINIMUM_LATENCY

public static final int PENDING_JOB_REASON_CONSTRAINT_MINIMUM_LATENCY

The minimum latency has not transpired.

Constant Value: 9 (0x00000009)

PENDING_JOB_REASON_CONSTRAINT_PREFETCH

public static final int PENDING_JOB_REASON_CONSTRAINT_PREFETCH

The system's estimate of when the app will be launched is far away enough to warrant delaying this job.

Constant Value: 10 (0x0000000a)

PENDING_JOB_REASON_CONSTRAINT_STORAGE_NOT_LOW

public static final int PENDING_JOB_REASON_CONSTRAINT_STORAGE_NOT_LOW

The requested storage-not-low constraint is not satisfied.

Constant Value: 11 (0x0000000b)

PENDING_JOB_REASON_DEVICE_STATE

public static final int PENDING_JOB_REASON_DEVICE_STATE

The job is being deferred due to the device state (eg. Doze, battery saver, memory usage, thermal status, etc.).

Constant Value: 12 (0x0000000c)

PENDING_JOB_REASON_EXECUTING

public static final int PENDING_JOB_REASON_EXECUTING

The job is currently running and is therefore not pending.

Constant Value: -1 (0xffffffff)

PENDING_JOB_REASON_INVALID_JOB_ID

public static final int PENDING_JOB_REASON_INVALID_JOB_ID

The job doesn't exist.

Constant Value: -2 (0xfffffffe)

PENDING_JOB_REASON_JOB_SCHEDULER_OPTIMIZATION

public static final int PENDING_JOB_REASON_JOB_SCHEDULER_OPTIMIZATION

JobScheduler thinks it can defer this job to a more optimal running time.

Constant Value: 13 (0x0000000d)

PENDING_JOB_REASON_QUOTA

public static final int PENDING_JOB_REASON_QUOTA

The app has consumed all of its current quota.

Constant Value: 14 (0x0000000e)

PENDING_JOB_REASON_UNDEFINED

public static final int PENDING_JOB_REASON_UNDEFINED

There is no known reason why the job is pending. If additional reasons are added on newer Android versions, the system may return this reason to apps whose target SDK is not high enough to expect that reason.

Constant Value: 0 (0x00000000)

PENDING_JOB_REASON_USER

public static final int PENDING_JOB_REASON_USER

JobScheduler is respecting one of the user's actions (eg. force stop or adb shell commands) to defer this job.

Constant Value: 15 (0x0000000f)

RESULT_FAILURE

public static final int RESULT_FAILURE

Returned from schedule(android.app.job.JobInfo) if a job wasn't scheduled successfully. Scheduling can fail for a variety of reasons, including, but not limited to:

  • an invalid parameter was supplied (eg. the run-time for your job is too short, or the system can't resolve the requisite JobService in your package)
  • the app has too many jobs scheduled
  • the app has tried to schedule too many jobs in a short amount of time

Attempting to schedule the job again immediately after receiving this result will not guarantee a successful schedule.

Constant Value: 0 (0x00000000)

RESULT_SUCCESS

public static final int RESULT_SUCCESS

Returned from schedule(android.app.job.JobInfo) if this job has been successfully scheduled.

Constant Value: 1 (0x00000001)

Public constructors

JobScheduler

public JobScheduler ()

Public methods

canRunLongJobs

public boolean canRunLongJobs ()

Returns true if the calling app currently holds the Manifest.permission.RUN_LONG_JOBS permission, allowing it to run long jobs.

Returns
boolean

cancel

public abstract void cancel (int jobId)

Cancel the specified job. If the job is currently executing, it is stopped immediately and the return value from its JobService#onStopJob(JobParameters) method is ignored.

Parameters
jobId int: unique identifier for the job to be canceled, as supplied to JobInfo.Builder(int, android.content.ComponentName).

cancelAll

public abstract void cancelAll ()

Cancel all jobs that have been scheduled by the calling application.

enqueue

public abstract int enqueue (JobInfo job, 
                JobWorkItem work)

Similar to schedule(JobInfo), but allows you to enqueue work for a new or existing job. If a job with the same ID is already scheduled, it will be replaced with the new JobInfo, but any previously enqueued work will remain and be dispatched the next time it runs. If a job with the same ID is already running, the new work will be enqueued for it without stopping the job.

The work you enqueue is later retrieved through JobParameters.dequeueWork. Be sure to see there about how to process work; the act of enqueueing work changes how you should handle the overall lifecycle of an executing job.

It is strongly encouraged that you use the same JobInfo for all work you enqueue. This will allow the system to optimally schedule work along with any pending and/or currently running work. If the JobInfo changes from the last time the job was enqueued, the system will need to update the associated JobInfo, which can cause a disruption in execution. In particular, this can result in any currently running job that is processing previous work to be stopped and restarted with the new JobInfo.

It is recommended that you avoid using JobInfo.Builder#setExtras(PersistableBundle) or JobInfo.Builder#setTransientExtras(Bundle) with a JobInfo you are using to enqueue work. The system will try to compare these extras with the previous JobInfo, but there are situations where it may get this wrong and count the JobInfo as changing. (That said, you should be relatively safe with a simple set of consistent data in these fields.) You should never use JobInfo.Builder#setClipData(ClipData, int) with work you are enqueuing, since currently this will always be treated as a different JobInfo, even if the ClipData contents are exactly the same.

Note: Scheduling a job can have a high cost, even if it's rescheduling the same job and the job didn't execute, especially on platform versions before version Build.VERSION_CODES.Q. As such, the system may throttle calls to this API if calls are made too frequently in a short amount of time.

Note: Prior to Android version Build.VERSION_CODES.UPSIDE_DOWN_CAKE, JobWorkItems could not be persisted. Apps were not allowed to enqueue JobWorkItems with persisted jobs and the system would throw an IllegalArgumentException if they attempted to do so. Starting with Build.VERSION_CODES.UPSIDE_DOWN_CAKE, JobWorkItems can be persisted alongside the hosting job. However, Intents cannot be persisted. Set a PersistableBundle using JobWorkItem.Builder#setExtras(PersistableBundle) for any information that needs to be persisted.

Note: The JobService component needs to be enabled in order to successfully schedule a job.

Parameters
job JobInfo: The job you wish to enqueue work for. See JobInfo.Builder for more detail on the sorts of jobs you can schedule. This value cannot be null.
work JobWorkItem: New work to enqueue. This will be available later when the job starts running. This value cannot be null.
Returns
int the result of the enqueue request. Value is RESULT_FAILURE, or RESULT_SUCCESS
Throws
IllegalArgumentException if the specified JobService doesn't exist or is disabled.

getAllPendingJobs

public abstract List<JobInfo> getAllPendingJobs ()

Retrieve all jobs that have been scheduled by the calling application.

Returns
List<JobInfo> a list of all of the app's scheduled jobs. This includes jobs that are currently started as well as those that are still waiting to run. This value cannot be null.

getPendingJob

public abstract JobInfo getPendingJob (int jobId)

Look up the description of a scheduled job.

Parameters
jobId int
Returns
JobInfo The JobInfo description of the given scheduled job, or null if the supplied job ID does not correspond to any job.

getPendingJobReason

public int getPendingJobReason (int jobId)

Returns a reason why the job is pending and not currently executing. If there are multiple reasons why a job may be pending, this will only return one of them.

Parameters
jobId int
Returns
int Value is PENDING_JOB_REASON_UNDEFINED, PENDING_JOB_REASON_APP, PENDING_JOB_REASON_APP_STANDBY, PENDING_JOB_REASON_BACKGROUND_RESTRICTION, PENDING_JOB_REASON_CONSTRAINT_BATTERY_NOT_LOW, PENDING_JOB_REASON_CONSTRAINT_CHARGING, PENDING_JOB_REASON_CONSTRAINT_CONNECTIVITY, PENDING_JOB_REASON_CONSTRAINT_CONTENT_TRIGGER, PENDING_JOB_REASON_CONSTRAINT_DEVICE_IDLE, PENDING_JOB_REASON_CONSTRAINT_MINIMUM_LATENCY, PENDING_JOB_REASON_CONSTRAINT_PREFETCH, PENDING_JOB_REASON_CONSTRAINT_STORAGE_NOT_LOW, PENDING_JOB_REASON_DEVICE_STATE, PENDING_JOB_REASON_EXECUTING, PENDING_JOB_REASON_INVALID_JOB_ID, PENDING_JOB_REASON_JOB_SCHEDULER_OPTIMIZATION, PENDING_JOB_REASON_QUOTA, or PENDING_JOB_REASON_USER

schedule

public abstract int schedule (JobInfo job)

Schedule a job to be executed. Will replace any currently scheduled job with the same ID with the new information in the JobInfo. If a job with the given ID is currently running, it will be stopped.

Note: Scheduling a job can have a high cost, even if it's rescheduling the same job and the job didn't execute, especially on platform versions before version Build.VERSION_CODES.Q. As such, the system may throttle calls to this API if calls are made too frequently in a short amount of time.

Note: The JobService component needs to be enabled in order to successfully schedule a job.

Parameters
job JobInfo: The job you wish scheduled. See JobInfo.Builder for more detail on the sorts of jobs you can schedule. This value cannot be null.
Returns
int the result of the schedule request. Value is RESULT_FAILURE, or RESULT_SUCCESS
Throws
IllegalArgumentException if the specified JobService doesn't exist or is disabled.