# Print output for @column tags ?>
public
abstract
class
JobScheduler
extends Object
java.lang.Object | |
↳ | android.app.job.JobScheduler |
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.
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.
Constants | |
---|---|
int |
RESULT_FAILURE
Returned from |
int |
RESULT_SUCCESS
Returned from |
Public constructors | |
---|---|
JobScheduler()
|
Public methods | |
---|---|
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 |
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. |
abstract
int
|
schedule(JobInfo job)
Schedule a job to be executed. |
Inherited methods | |
---|---|
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:
JobService
in your package)Constant Value: 0 (0x00000000)
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 JobScheduler ()
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) . |
public abstract void cancelAll ()
Cancel all jobs that have been scheduled by the calling application.
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.
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 enqueue, 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: 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. |
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 . |
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. |
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. |