Android Lollipop was a huge milestone for Android. It introduced us to Android Wear, gave us a do-not-disturb feature, and unleashed material design upon the world. Not only that, it also gave us Project Volta, a set of changes aimed at improving battery life.
One of the pieces of Project Volta is a set of job scheduling APIs that let developers schedule work to be done when certain conditions are met instead of at a specified time. So rather than scheduling a download for 1am and needing to worry about whether the device is charging or on an expensive data connection, we can just use the JobScheduler and tell it not to start our job until the device is charging and on Wi-Fi.
Luckily, using the JobScheduler is pretty straightforward. There’s only three things you need to do:
- Create a JobService to handle your job
- Add that JobService to the manifest
- Schedule your job using a JobInfo object to define your conditions
Contents
Creating a JobService
JobService is a specialized Service (i.e. extends Service) with a couple methods to help us handle our job. To create a JobService, start by extending the JobService class and overriding ‘onStartJob’ and ‘onStopJob’. OnStartJob is called by the Android system when it starts your job. However, onStopJob is only called if the job was cancelled before being finished (e.g. we require the device to be charging, and it gets unplugged).
Also, when your job is finished (has been completed or canceled) you’re responsible for calling the ‘jobFinished’ method. The ‘jobFinished’ method tells Android that your job is done and lets it release the wakelock for your app. If you don’t call the ‘jobFinished’ method your app could be responsible for draining your user’s battery!
Here’s an example of a simple JobService that does 10 seconds of ‘work’ on a new Thread:
Adding the JobService to the Manifest
Since a JobService IS a Service, you’ll need to declare it in your manifest. The only catch is that since it’s a JobService you also need to include the BIND_JOB_SERVICE permission.
Scheduling the Job
When it’s time to schedule your job you’ll need to start with a JobInfo object. A JobInfo object contains all your job details and is used by the JobScheduler to schedule your job. To create a JobInfo you’ll need to use a JobInfo.Builder and supply it with a ‘jobId’ (integer of your choosing) and the ComponentName of your JobService. Then you’ll add any conditions you need (e.g. setPeriodic), and build the JobInfo object:
Once you’ve got your JobInfo, all that’s left is scheduling your job. Just grab a reference to the JobScheduler and call the ‘schedule’ method!
A good way to test that this works would be to run the app on a connected device and then turning the WiFi off to see the job get cancelled. Then turn the WiFi back on, and after a couple minutes you should be able to see your job be restarted. So the next time you’re planning a download in the background, think about giving your users a little more battery life by using the JobScheduler!
Also, if you’re looking to learn more about Android development, then I suggest you start here. We’ll take you from novice to Android developer one step at a time and explain everything along the way. On the other hand, if you’re looking for something a bit more advanced, then I strongly encourage you to check out our Threads and Services course. It’ll give you all the information you need to feel confident building apps that do work in the background!
Resources
- GitHub Project
- Scheduling jobs like a pro with JobScheduler – Android Developers Video / Blog Post
- Intelligent Job Scheduling – Android Developer Guide
- JobScheduler – Docs
- JobInfo.Builder – Docs
Scheduling Time-Sensitive Tasks in Android with the AlarmManager
Hey guys, thanks so much for this post. I’ve been scanning a ton of documentation to get a proper grasp on the JobScheduler, and found this the most helpful. Cheers!