The document discusses Android job scheduling and provides details about the JobScheduler API. It defines scheduling, compares the AlarmManager and JobScheduler, and describes how to implement and schedule jobs using the JobScheduler. Examples are provided to demonstrate how to create a JobService, schedule jobs with criteria like network availability, and ensure jobs are persisted across device reboots.
3. DEFINITION:
First we have to know what is scheduling, “ The process of systematically arranging
numerical content in horizontal or vertical columns is called scheduling”.
Android scheduling is a module that contains logic that detects whether or not the device is
in an idle state, and may be rebooted without user disruption.
The Android system currently has two main means to schedule tasks:
the (outdated) Alarm Manager
the JobScheduler API.
4. ALARM MANAGER
Android Alarm Manager allows you to access system alarm.
By the help of Android Alarm Manager in android, you can schedule your application to
run at a specific time in the future.
It works whether your phone is running or not.
The Android Alarm Manager holds a CPU wake lock that provides guarantee not to
sleep the phone until broadcast is handle
but it is harder for noobs so google make things easy for us. now we talk about job
scheduler
Modern Android applications should use the JobScheduler API. Apps can schedule jobs
while letting the system optimize based on memory, power, and connectivity conditions
5. JOBSCHEDULER
The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the
JobScheduler class. 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 Android system can combine jobs so that battery consumption
is reduced. JobManager makes handling uploads easier as it handles automatically the
unreliability of the network. It also survives application restarts. Here are example when
you would use this job scheduler:
6. USES
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
7. WORKING
A unit of work is encapsulated by a JobInfo object. This object specifies the scheduling criteria.
The job scheduler allows to consider the state of the device, e.g., if it is idle or if network is
available at the moment. Use the JobInfo.Builder class to configure how the scheduled task
should run. 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
8. CONT(WORKING)
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
These constraints can be combined. For example, you can schedule a job every 20 minutes,
whenever the device is connected to an unmetered network. Deadline is a hard constraint,
if that expires the job is always scheduled.
To implement a Job, extend the JobService class and implement the onStartJob and
onStopJob. If the job fails for some reason, return true from on the onStopJob to restart
the job. The onStartJob is performed in the main thread, if you start asynchronous
processing in this method, return true otherwise false. Here is the example of Job
Scheduler :
9. EXAMPLE
class MyJobScheduler : JobService(){
override fun onStartJob(p0: JobParameters?): Boolean {
return false
}
override fun onStopJob(p0: JobParameters?): Boolean {
return false
}
}
10. CONT(EXAMPLE)
OnStartJob :
onStartJob will be called when the job execution gets started. It takes JobParameters as a parameter.
You can the job info like job id by using that parameter. The return type for this job is Boolean that
means you have to inform the system whether you want to start the job again after finishing their job or
not. If you want to cancel the job after finishing the execution then simply return false which means the
system will call onStopJob method and automatically cancel the specified job.
OnStopJob :
onStop Job will be called by the system. When the service is running and some of the criteria are not
satisfying the service then the android system will automatically call onStop Job. Let suppose you have
registered a job scheduler that required a network connection and your service is running. Then you
disable your net connection. Then the android system will call the onStopJob. It is also returning a
Boolean value that means you want to register the job again or you want to let it finish.
11. CONT
If you want your service to be prevented from booting or Restarting the android device so you
have to add boot complete permission and set your job setPersisted(true) in your JobInfo.
android:permission="android.permission.RECEIVE_BOOT_COMPLETED“
Now the last part comes into the picture execution of the service.
12. CONT
class MainActivity : AppCompatActivity() {
val JOB_ID = 1001;
val REFRESH_INTERVAL : Long = 15 * 60 * 1000 // 15 minutes
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
scheduleJob()
}
private fun scheduleJob() {
val jobScheduler = applicationContext
.getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
val componentName = ComponentName(this, MyJobScheduler::class.java)
val jobInfo = JobInfo.Builder(JOB_ID, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(REFRESH_INTERVAL)
.build()
jobScheduler.schedule(jobInfo)
}
}
13. CONT
First of all, we have to take system services for “JOB_SCHEDULER_SERVICE”.
Then we have to create our component name object that takes two parameters
first is context and the second is your Service class.
The last and main part is JobInfo you have to build your JobInfo object by using
JobInfo.Builder class. It will contain all your information regarding the job like
the job id all the criteria that are required for your service.
I suggest you read all the criteria that JobInfo provides to be a master in
scheduling the jobs.
Here we have done. Hope this blog will help you to understand the Job Scheduler
in Android.