What is Job?
First of all, we have to understand, what is Job and then we can able to understand the rest of the things clearly.
So Job is nothing but the task or work which we want to do. For example, if we consider our daily works, then brushing out teeth, washing dresses, cooking food, are nothing but works or tasks or jobs which we have to finish. So in our programing, jobs or tasks are the same as work. For example ‘fetching images from the server’, ‘sending locations to the server in some interval’, start or stop the music, etc.
What is JobScheduling?
Let’s consider the same real-life example. We can’t do ‘brushing our teeth’ and ‘washing dresses’ at a time. So we have to assign some specific time to each work. That is called Scheduling work. In programming, its called scheduling a job or job scheduling.
What is JobScheduler?
Well, JobScheduler is an Android API to schedule jobs that will run in applications’ own process. Application’s own process means the main process Or thread in which the application is running. But we should have to help the JobScheduler by providing each job information which is called JobInfo. Otherwise, how the JobScheduler will know which task to do and when right.
What is JobInfo?
JobInfo is nothing but the description of each task to do. It has one unique Id which will help the JobScheduler to recognize each task. It also has the scheduling time, which will help the JobScheduler to run this job in that time interval (set periodic(milliseconds)). It also has many extra settings like whether to run only on wifi (setRequirNetworkType()), whether to run these JobScheduler when the device is in charging mode or not (set requires charging()) and many more.
Why use JobScheduler?
And has its own components. Service is one of them. Service helps to do continuous operations when the application is both in foreground (i.e visible to the user) or background (i.e not visible to the user but still running). But it affects both device performance and battery by capturing the resource and wake lock(it keeps the processor wake) until it stops. So from Android Oreo, Google recommended not to use any continuous running service which affects the battery.
Instead of Service, Google recommends using JobScheduler which will run the task in some time interval (minimum 15 mins). JobScheduler has two methods. onStartJob() and onStopJob().
How to start a JobScheduler?
To schedule tasks in JobScheduler, we have to write one service class which will extend JobService instead of plain Service. Then we have to override both onStartJob() and onStopJob(). Now we have to write our task inside onStartJob() which is responsible to start the job. And once we have done, we have to finish our task inside onStopJob() which will help the wake lock to release from the system.
So let’s start some programs to know how actually we have to use this JobScheduler. Here I am updating some analytics data in each 15 mins interval. Below I pasted the AlalyticsService class which is sending some analytics data to the server in each 15 mins interval.
As this is one Service class, we have to define it inside our manifest with BIND_JOB_SERVICE permission, which will help the service to tightly couple with the app. Just like below. And also make sure to use Minimum Android Version as 21.
Schedule the Job:
To schedule the job, we have to use ComponentName instead of an Intent. So what is ComponentName? Well, ComponentName is an identifier of the component that is available.
Then we have to create the JobInfo with the job details. I pasted the Application class below where I started the Analytics service.