I'm building an Android application, which makes use of a manifest broadcast receiver to know when the device is plugged in. I think Android 5.0 JobScheduler API would be a much better way of doing this, but I have one question: is it possible to use this API or a similar one on earlier versions of Android? If it is not possible, what would be the best option to do this (instead of a manifest broadcast receiver)?
6 Answers
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)

- 4,982
- 3
- 31
- 34
-
great job man, I love this library. Unfortunately there isn't much documentation about it and I'm offen find myself not sure about the configuration of the job. – Nativ Feb 24 '16 at 09:48
-
Actually, you shouldn't care much about the configuration. The documentation on Github explains everything that is needed. Creating an actual job also isn't hard. Create your Job class and schedule it with the Builder, which provides some easy to use parameters. – vRallev Feb 25 '16 at 16:34
-
WorkManager for deferrable tasks is the recommended way, please see my answer – Alok Feb 17 '19 at 09:13
Firebase JobDispatcher was introduced.
This replaces the old GCM Network Manager library.

- 843
- 7
- 15
-
I tried GCM & Firebase, and I found times where the Job wasn’t fired for hours. – ucMedia Apr 25 '18 at 11:18
-
WorkManager for deferrable tasks is the recommended way, please see my answer – Alok Feb 17 '19 at 09:14
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

- 18,291
- 10
- 66
- 81
-
1But then Google introduced https://developers.google.com/cloud-messaging/network-manager – Tobrun Jun 16 '15 at 09:41
-
WorkManager for deferrable tasks is the recommended way, please see my answer – Alok Feb 17 '19 at 09:13
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.

- 986,068
- 189
- 2,389
- 2,491
-
Thank you! I simply want to do some work while phone is charging, to reduce battery consumption. The work is done inside an IntentService – fergaral Oct 18 '14 at 16:51
-
@user3194230: That definitely sounds like a good use case for `JobScheduler` (though you'd need to switch to a `JobService` for that). You can perhaps [use `ACTION_POWER_CONNECTED`](http://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_CONNECTED) in the meantime, though -- I had forgotten about that broadcast when I wrote my answer. – CommonsWare Oct 18 '14 at 17:05
-
Yes. Right now I'm using a manifest BroadcastReceiver that listens for the ACTION_POWER_CONNECTED broadcast. On my Nexus 5 running Android 4.4.4 it works as expected, but I didn't get it to work on my friend's Samsung Galaxy S (running Android 2.3.6). I have tried listening for the ACTION_BATTERY_CHANGED broadcast, and it works both in my phone and in my friend's one, but this broadcast requires my app to be running all the time – fergaral Oct 19 '14 at 23:32
-
@user3194230: "I didn't get it to work on my friend's Samsung Galaxy S" -- well, the broadcast was available then, so Samsung must have screwed something up. :-( "this broadcast requires my app to be running all the time" -- yes, because if they had to fork a bunch of processes every time the battery dropped a bit of charge, that would be bad for the battery. Hence, you have to have a running process. – CommonsWare Oct 19 '14 at 23:38
-
Thank you. 'Hence,you have to have a running process'. What would be the best way of implementing it? Maybe using an AlarmManager? – fergaral Oct 21 '14 at 13:40
-
@user3194230: That's what I suggested in my answer. You can find out whether or not you are plugged in when you are woken up with the alarm: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html#DetermineChargeState – CommonsWare Oct 21 '14 at 16:49
-
And... is there any other way of doing this? Because, if I implement an AlarmManager, I would need to set up a broadcast receiver to schedule my alarms when the device boots, and I'm having a little trouble with this receiver in my friend's phone (its onReceive method never gets called). In my phone it works as expected. – fergaral Oct 21 '14 at 23:00
-
@user3194230: "is there any other way of doing this?" -- you have exhausted all of my ideas, sorry. – CommonsWare Oct 21 '14 at 23:15
-
-
@CommonsWare could you help me on this one please http://stackoverflow.com/questions/29047144/background-process-to-scan-the-location-of-the-user-at-regular-intervals-and-upd – Sagar Devanga Mar 16 '15 at 06:37
-
WorkManager for deferrable tasks is the recommended way, please see my answer – Alok Feb 17 '19 at 09:13
Important Update
For Now onward everybody should use WorkManager for deferrable background tasks.
- This library is backward compatible
- It use JobScheduler,FirebaseJobDispatcher or AlarmManager
- No Need to depend on play service library.
- Recommended by Google for deferrable background work.
- Can use features like chaining, constraints etc.
https://developer.android.com/guide/background/#workmanager https://developer.android.com/topic/libraries/architecture/workmanager/

- 881
- 1
- 11
- 18
Came across this library, didn't try it, but looks good for what you want: https://github.com/airk000/Trigger
Evernote android-job might be a better solution though :)

- 1,244
- 2
- 15
- 31