public static final class JobRequest.Builder extends Object
Constructor and Description |
---|
Builder(String tag)
Creates a new instance to build a
JobRequest . |
Modifier and Type | Method and Description |
---|---|
JobRequest.Builder |
addExtras(PersistableBundleCompat extras)
Adds optional extras.
|
JobRequest |
build() |
boolean |
equals(Object o) |
int |
hashCode() |
JobRequest.Builder |
setBackoffCriteria(long backoffMs,
JobRequest.BackoffPolicy backoffPolicy)
Change the back-off policy for a non periodic job.
|
JobRequest.Builder |
setExact(long exactInMs)
Specify that the job should run at an exact time.
|
JobRequest.Builder |
setExecutionWindow(long startInMs,
long endInMs)
Set the time window when the job will be run.
|
JobRequest.Builder |
setExtras(PersistableBundleCompat extras)
Set optional extras.
|
JobRequest.Builder |
setPeriodic(long intervalMs)
Specify that this job should recur with the provided interval, not more than once per period.
|
JobRequest.Builder |
setPeriodic(long intervalMs,
long flexMs)
Specify that this job should recur with the provided interval and flex, not more than once per period.
|
JobRequest.Builder |
setRequiredNetworkType(JobRequest.NetworkType networkType)
Set some description of the kind of network type your job needs to have.
|
JobRequest.Builder |
setRequirementsEnforced(boolean enforced)
It's possible to set several requirements for a job, however, not all of them need to
be considered by the underlying
JobApi . |
JobRequest.Builder |
setRequiresBatteryNotLow(boolean requiresBatteryNotLow)
Specify that to run this job, the device battery shouldn't be below a curtain threshold.
|
JobRequest.Builder |
setRequiresCharging(boolean requiresCharging)
Specify that to run this job, the device needs to be plugged in.
|
JobRequest.Builder |
setRequiresDeviceIdle(boolean requiresDeviceIdle)
Specify that to run, the job needs the device to be in idle mode.
|
JobRequest.Builder |
setRequiresStorageNotLow(boolean requiresStorageNotLow)
Specify that to run this job, the device storage shouldn't be low.
|
JobRequest.Builder |
setTransientExtras(Bundle extras)
Set optional transient extras.
|
JobRequest.Builder |
setUpdateCurrent(boolean updateCurrent)
Sets whether this request should overwrite any preexisting jobs with the same tag.
|
JobRequest.Builder |
startNow()
Specify that the job should start immediately.
|
final String mTag
public Builder(@NonNull String tag)
JobRequest
. Note that the tag
doesn't
need to be unique. Each created request has an unique ID to differentiate between jobs
with the same tag.
JobCreator
to create
a Job
for this tag
.tag
- The tag is used to identify your Job
in JobCreator.create(String)
.public JobRequest.Builder setExecutionWindow(long startInMs, long endInMs)
setPeriodic(long)
or setExact(long)
. For those types jobs it doesn't
make sense to have a time window.
System.currentTimeMillis() + startInMs
and
System.currentTimeMillis() + endInMs
.
Long.MAX_VALUE / 3 * 2
(about 53_375_995_583 days).
Otherwise some APIs schedule the job immediately. No exception is thrown if an argument is greater
than the maximum value, the arguments are silently being clamped.
AlarmManager
used
as fallback API doesn't allow setting a start date. Although being inexact, the execution time is
the arithmetic average of startInMs
and endInMs
. The result could be that your job never
runs on pre Android 5.0 devices, if one argument is too large.startInMs
- Earliest point from which your task is eligible to run.endInMs
- Latest point at which your task must be run.public JobRequest.Builder setExtras(@Nullable PersistableBundleCompat extras)
extras
- Bundle containing extras which you can retrieve with Job.Params.getExtras()
.public JobRequest.Builder addExtras(@NonNull PersistableBundleCompat extras)
extras
, which were set before.extras
- Bundle containing extras which you can retrieve with Job.Params.getExtras()
.public JobRequest.Builder setRequirementsEnforced(boolean enforced)
JobApi
. If the requirements are enforced, then
the device state is checked before your job runs. If at least one requirement isn't met,
then the job is rescheduled and not run.
false
. The Job
class provides several methods, e.g.
Job.isRequirementChargingMet()
.
false
.enforced
- If true
, then all set requirements are manually checked.public JobRequest.Builder setRequiredNetworkType(@Nullable JobRequest.NetworkType networkType)
JobRequest.NetworkType.ANY
.
networkType
- The required network type.setRequirementsEnforced(boolean)
,
setExecutionWindow(long, long)
public JobRequest.Builder setRequiresCharging(boolean requiresCharging)
false
.
requiresCharging
- Whether or not the device needs to be plugged in.setRequirementsEnforced(boolean)
,
setExecutionWindow(long, long)
public JobRequest.Builder setRequiresDeviceIdle(boolean requiresDeviceIdle)
false
. Idle mode is a loose definition provided by the system, which means that the device
is not in use, and has not been in use for some time. As such, it is a good time to
perform resource heavy jobs.
requiresDeviceIdle
- Whether or not the device needs be idle.setRequirementsEnforced(boolean)
,
setExecutionWindow(long, long)
public JobRequest.Builder setRequiresBatteryNotLow(boolean requiresBatteryNotLow)
false
.
requiresBatteryNotLow
- Whether or not the device batter shouldn't be low.setRequirementsEnforced(boolean)
,
setExecutionWindow(long, long)
public JobRequest.Builder setRequiresStorageNotLow(boolean requiresStorageNotLow)
false
. Note: This requirement only has an
affect on Android O, but not lower versions. It's never guaranteed that enough
space is available, when your job runs. This is more like a hint.
requiresStorageNotLow
- Whether or not the device storage shouldn't be low.setRequirementsEnforced(boolean)
,
setExecutionWindow(long, long)
public JobRequest.Builder setExact(long exactInMs)
false
. Internally an exact job is always using the
AlarmManager
.
System.currentTimeMillis() + exactInMs
.
Long.MAX_VALUE / 3 * 2
(about 53_375_995_583 days).
No exception is thrown if the argument is greater than the maximum value, the argument is
silently being clamped.exactInMs
- The exact offset when the job should run from when the job was scheduled.AlarmManager.setExact(int, long, android.app.PendingIntent)
,
AlarmManager.setExactAndAllowWhileIdle(int, long, android.app.PendingIntent)
public JobRequest.Builder startNow()
Service
is that jobs run in parallel and can be rescheduled if necessary. It
also respect the background execution limit introduced in Android O, meaning that if
it's not allowed to start a Service
, then the work is delayed.setExact(long)
public JobRequest.Builder setPeriodic(long intervalMs)
Job.Result.RESCHEDULE
is ignored for periodic jobs,
setting a back-off criteria is illegal as well.intervalMs
- The job should run at most once every intervalMs
. The minimum value is 15min
.public JobRequest.Builder setPeriodic(long intervalMs, long flexMs)
Job.Result.RESCHEDULE
is ignored for
periodic jobs, setting a back-off criteria is illegal as well.intervalMs
- The job should run at most once every intervalMs
. The minimum value is 15min
.flexMs
- How close to the end of the period the job should run. The minimum value is 5min
.JobRequest.MIN_INTERVAL
,
JobRequest.MIN_FLEX
public JobRequest.Builder setBackoffCriteria(long backoffMs, @NonNull JobRequest.BackoffPolicy backoffPolicy)
JobRequest.BackoffPolicy.EXPONENTIAL
. The time is increasing each time a job fails and
returns Job.Result.RESCHEDULE
, but capped at 5 hours.
backoffMs
- The initial interval to wait when the job has been rescheduled.backoffPolicy
- Is either JobRequest.BackoffPolicy.LINEAR
or JobRequest.BackoffPolicy.EXPONENTIAL
.Job.Result.RESCHEDULE
,
Job.onReschedule(int)
public JobRequest.Builder setUpdateCurrent(boolean updateCurrent)
true
,
then this request calls JobManager.cancelAllForTag(String)
with the given tag before
being scheduled.updateCurrent
- If true
this request will cancel any preexisting job with the same tag
while being scheduled.public JobRequest.Builder setTransientExtras(@Nullable Bundle extras)
null
or empty, then the previous extras are reset to the default
and the job won't be transient.extras
- Bundle containing extras you want the scheduler to hold on to for you.public JobRequest build()
JobRequest
with this parameters to hand to the JobManager
.