JobScheduler and the future of background work
JobScheduler and the future of background work
In this chapter, we showed you how to implement background work without JobScheduler. Because
JobScheduler is only available on Lollipop and later, and there is no support library version available,
the AlarmManager-based solution shown in this chapter is the only approach from the standard libraries
that will work across all the versions of Android that PhotoGallery supports.
It is important to note, though, that AlarmManager’s days of performing this kind of work are probably
numbered. One of the highest priority goals for Android platform engineers in recent years has been
improving power efficiency. To do this, they have sought greater control on scheduling when apps use
the radio, WiFi, and other tools that can run through a battery quickly.
This is why AlarmManager’s commands have changed meaning over the years: Android knows that
developers use AlarmManager to schedule background work, so those APIs have been loosened and
tweaked to try to make your app play nicely with others.
At its heart, though, AlarmManager is a bad API for this purpose. It tells Android nothing about what
you are doing – you could be using the GPS radio or you could be updating the look of an app widget
on your user’s Home screen. Android does not know, so it must treat all alarms identically. That keeps
Android from making intelligent choices about power consumption.
This pressure means that as soon as JobScheduler is an option for most apps, AlarmManager will
fall out of favor. So while JobScheduler is not a compatible option today, we strongly recommend
switching your applications to use it as soon as you determine it is feasible.
If you want to do something today instead of planning an API switch in the future, you can use a
third-party compatibility library instead. As of this writing, Evernote’s android-job library is the best
option. You can find it at github.com/evernote/android-job.
Challenge: Using JobService on Lollipop
For an additional challenge, create a second implementation of PollService that subclasses
JobService and is run using JobScheduler. In your PollService startup code, check to see whether
you are on Lollipop. If so, use JobScheduler to schedule your JobService. Otherwise, fall back on
your old AlarmManager implementation.
For the More Curious: Sync Adapters
Yet another way to set up a regularly polling web service is to use a sync adapter. Sync adapters are
not adapters like you have seen before. Instead, their sole purpose is to sync data with a data source
(uploading, downloading, or both). Unlike JobScheduler, sync adapters have been around for a while,
so you do not have to worry about which version of Android you are running.
Like JobScheduler, sync adapters can be used as a replacement for the AlarmManager setup that you
have in PhotoGallery. Syncs from multiple applications are grouped together by default, without you
having to set flags a certain way. Furthermore, you do not have to worry about resetting the sync alarm
across reboots because sync adapters handle this for you.