Android 5.0 JobScheduler API on earlier versions?
I think Android 5.0 JobScheduler API would be a much better way of doing this
Well, that depends on what "this" is. Simply knowing when the device is plugged in is pointless unless you are going to do something with that information, and we do not know what you have in mind.
Bear in mind that JobScheduler
does not necessarily give you control when the device is plugged in. Rather, it uses that fact, plus your JobInfo
and other environmental factors, to determine when to give you control.
is it possible to use this API or a similar one on earlier versions of Android?
There is no backport of JobScheduler
supplied by the Android SDK at this time. While somebody will likely write a backport at some point, I am not aware of one available as open source at the moment either.
If it is not possible, what would be the best option to do this (instead of a manifest broadcast receiver)?
Again, it depends on what "this" really is. For example, if you are trying to say "I want to get control every so often, but only if the device is plugged in", you could use AlarmManager
, then check to see whether the device is plugged in before continuing with your work.
Update:
WorkManager was introduced to replace below.
Update:
Firebase JobDispatcher was introduced to replace below.
Update:
In Play Services 7.5 GCM Network Manager was introduced.
This is the closest approximation for a backport of the JobSchedulers API.
Evant user on Github is creating an Compat version of the JobScheduler API.
At time of posting it is not production ready but it is worth mentioning
Another option is this library: https://github.com/evernote/android-job
It uses the JobScheduler
and GcmNetworkManager
if possible. Otherwise it fallbacks to the AlarmManager
. It also handles different API levels with the AlarmManager
properly. This blog post gives you an overview.
(PS: I'm the main developer who worked on this library at Evernote. It's useful for our projects, that's why I wanted to share it here as well)
Firebase JobDispatcher was introduced.
This replaces the old GCM Network Manager library.