Android Lollipop – JobScheduler API

Thanks to google that he has introduced Job Scheduling API in its latest version Android Lollipop. Sometimes we may face the situation where we want to run a particular task later on some specific condition (known as scheduling). We can do this using the JobScheduler APIs provided by Android 5.0 onward now.

In this tutorial, I will explain you how to use the JobScheduler API to create jobs which will execute in the background only when some specific conditions occur. You can check the API level details here on JobScheduler Android developer’s site. The API is able to batch different jobs at once and run them together with the consideration to save a device’s battery life. This tutorial will show you how to schedule a job running in the background on a button click. The user will be able to cancel it any time on button click.

Follow the steps I will describe below to understand how this all works. Also, please make sure to set your minimum SDK to API 21.

Step1: Create a new android application in your android IDE (assuming you will be using android studio) with minimus SDK version 5.0.

Step2: Write following into main layout file:

 

 

 

Step3: Create and write following into SchedulerService.java:

 

 

 

Step4: Add below line to your manifest:

 

 

 

Step5: Finally add these jobs with your main activity. Write below into your main activity file:

 

 

 

OutPut

jobscheduler1

jobscheduler2