Job Scheduler in Android

Updated 31 December 2021

Save

Job Scheduler in Android

In this blog, We are going to learn about Job Scheduler. JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory.

So if you want to perform some background job like some network operations no matter your app is running or not. Here comes the Job scheduler. You can take this as a modern Alarm manager. The job scheduler is a more intelligent alarm manager. If you have some operation that is based on time then you have to use the alarm manager in that scenario. But Job Scheduler has its own criteria like If the device connected with wifi or network connection or it is currently charging.

Usually, It will batch different jobs together to be more efficient. The job executes on the basis of that criteria.

We can create a JobScheduler by extending a class from JobService. It is a substitute of Intent service while the intent service gets deprecated. JobService used main thread by default we have to manage the threads while doing long-running jobs like network operations.

Here is the example of Job Scheduler :

We will have to override two methods.

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.

Here we have to do some additional changes to our Job Scheduler.

<service android:name=”.MyJobScheduler” android:permission=”android.permission.BIND_JOB_SERVICE” />

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.

Now the last part comes into the picture execution of the service.

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.

Reference link –> https://developer.android.com/reference/android/app/job/JobScheduler,

https://developer.android.com/reference/android/app/job/JobInfo,

https://developer.android.com/reference/android/app/job/JobService

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home