2. JobScheduler
The job scheduler API
The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobSchedulerclass. This
API allows to batch jobs when the device has more resources available. In general this API can be used
to schedule everything that is not time critical for the user.
Compared to a custom SyncAdapter or the alarm manager, the JobScheduler supports batch scheduling
of jobs
The JobScheduler API performs an operation for your application when a set of predefined conditions are
met. Unlike the AlarmManager class, the timing isn't exact. In addition, the JobScheduler API is able to
batch various jobs to run together. This allows your app to perform the given task while being considerate
of the device's battery at the cost of timing control.
Tasks that should be done once the device is connect to a power supply
Tasks that require network access or a Wi-Fi connection.
Task that are not critical or user facing
Tasks that should be running on a regular basis as batch where the timing is not critical
3. You can schedule the task to run under specific conditions,
such as:
Device is charging
Device is connected to an unmetered network
Device is idle
Start before a certain deadline
Start within a predefined time window, e.g., within the next
hour
Start after a minimal delay, e.g., wait a minimum of 10
minutes
4. 1. Creating the Job Service
public class JobSchedulerService extends JobService
android:permission="android.permission.BIND_JOB_SERVICE"
which requires that two methods be created onStartJob (JobParameters params) and onStopJob (JobParameters params).
onStartJob(JobParameters params) is the method that you must use when you begin your task, because it is what the system uses to
trigger jobs that have already been scheduled. As you can see, the method returns a boolean value. If the return value is false, the system
assumes that whatever task has run did not take long and is done by the time the method returns. If the return value is true, then the
system assumes that the task is going to take some time and the burden falls on you, the developer, to tell the system when the given task
is complete by calling jobFinished(JobParameters params, boolean needsRescheduled).
onStopJob(JobParameters params) is used by the system to cancel pending tasks when a cancel request is received.
One thing to note is that the job service runs on your application's main thread
5. onStartJob(JobParameters params) is the method that you must use when you begin your task,
because it is what the system uses to trigger jobs that have already been scheduled.
onStopJob(JobParameters params) is used by the system to cancel pending tasks when a cancel
request is received.
When the task is done, you need to call jobFinished(JobParameters params, boolean
needsRescheduled) to let the system know that you're done with that task and that it can begin
queuing up the next operation. If you don't do this, your jobs will only run once and your
application will not be allowed to perform additional jobs.
6. 2. Creating the Job Scheduler
The first thing you will need to do is create a JobScheduler object,
initialize it by getting an instance of the system service JOB_SCHEDULER_SERVICE.
JobInfo
public class JobInfo
extends Object implements Parcelable
java.lang.Object android.app.job.JobInfo↳
Container of data passed to the JobScheduler fully encapsulating the parameters required to schedule work against the calling application.
These are constructed using the JobInfo.Builder. You must specify at least one sort of constraint on the JobInfo object that you are
creating. The goal here is to provide the scheduler with high-level semantics about the work you want to accomplish. Doing otherwise with
throw an exception in your app.
.
7.
8. setMinimumLatency(long minLatencyMillis): This makes your job not launch until the stated number of milliseconds have
passed. This is incompatible with setPeriodic(long time) and will cause an exception to be thrown if they are both used.
setOverrideDeadline(long maxExecutionDelayMillis): This will set a deadline for your job. Even if other requirements are
not met, your task will start approximately when the stated time has passed. Like setMinimumLatency(long time), this
function is mutually exclusive with setPeriodic(long time) and willcause an exception to be thrown if they are both used.
setPersisted(boolean isPersisted): This function tells the system whether your task should continue to exist after the
device has been rebooted.
setRequiredNetworkType(int networkType): This function will tell your job that it can only start if the device is on a
specific kind of network. The default is JobInfo.NETWORK_TYPE_NONE, meaning that the task can run whether there
is network connectivity or not. The other two available types are JobInfo.NETWORK_TYPE_ANY, which requires some
type of network connection available for the job to run, and JobInfo.NETWORK_TYPE_UNMETERED, which requires
that the device be on a non-cellular network.
setRequiresCharging(boolean requiresCharging): Using this function will tell your application that the job should not start
until the device has started charging.
setRequiresDeviceIdle(boolean requiresDeviceIdle): This tells your job to not start unless the user is not using their
device and they have not used it for some time.
13. Alarm Manager
public class AlarmManager
extends Object
java.lang.Object android.app.AlarmManager↳
This class provides access to the system alarm services. These allow you to
schedule your application to be run at some point in the future. When an alarm
goes off, the Intent that had been registered for it is broadcast by the system,
automatically starting the target application if it is not already running. Registered
alarms are retained while the device is asleep (and can optionally wake the
device up if they go off during that time), but will be cleared if it is turned off and
rebooted.