I was looking at a scheduling tutorial by Vogella. It mentions the Job Scheduler API that was introduced in API 21 of Android. My question is can it be implemented in APIs lower than 21 (Lollipop) but not less than Android version 3 (Honeycomb)?
-
1I believe this is still relevant answer ; http://stackoverflow.com/a/27633880/680519 . – harism Feb 17 '15 at 14:23
-
in the mentionned tutorial, he compares the job scheduler to the alarm manager and the sync adapter, both solutions that exist in lower apis and have similar functionalities. – njzk2 Feb 17 '15 at 14:25
-
1Based on exactly what you're doing, there are different options, check out the background process guide: https://developer.android.com/guide/background/ – MrGrigg Nov 21 '19 at 20:17
7 Answers
from now on (after I/O 2015), you can also use new GcmNetworkManager. How to use it and how it works is described here - https://developers.google.com/cloud-messaging/network-manager
It does a lot cool stuff like it persists your tasks trough reboots. On Lolipop it uses JobScheduler, on pre-Lolipop it uses it's own implementation.
EDIT:
An example code on how to use it : https://github.com/jacktech24/gcmnetworkmanager-android-example

- 725
- 6
- 15
-
Is it related to GCM in any way? Can any app, even those that don't have anything to do with GCM, use it? Can it be a total replacement of JobScheduler? What are the disadvantages? – android developer Jul 14 '15 at 13:34
-
6It's not related to GCM in any way you have to care about. It's not replacement of job scheduler, cause this is only for internet related tasks, but it is a way for you to get network related Job Scheduler on older Android versions – filipproch Jul 14 '15 at 13:38
-
2Can it also be used for non-interenet-related stuff? Also, do you know of a good sample of how to use it (other than the link you've provided) ? – android developer Jul 14 '15 at 13:43
-
I am now on my phone, but in an hour, i will be at my PC so I will give you an example. – filipproch Jul 14 '15 at 13:48
-
If it's too long, can you please post it to Github? I think a lot of people would like this. – android developer Jul 14 '15 at 13:50
-
Thank you. Does it also require Google Play services to be available? Or can it run even without it? Also, about gist, I prefer showing it here then. :) – android developer Jul 14 '15 at 13:52
-
It requires GP Services, because they are the thing that calls your service on pre-Lollipop versions – filipproch Jul 14 '15 at 13:53
-
2I see. But can it run even without any relation to the internet? For example, scanning some internal stuff of the app on the device? – android developer Jul 14 '15 at 13:57
-
It's targeted on network stuff, but what you do in there is up to you. – filipproch Jul 14 '15 at 13:59
-
-
jacktech24 is absolutely correct. There is however a way to specify that your task doesn't need network - https://developers.google.com/android/reference/com/google/android/gms/gcm/Task.Builder.html#setRequiredNetwork(int) with NETWORK_STATE_ANY – 43matthew Jul 14 '15 at 18:12
-
@jacktech24 code seems to work. I think you should have put it in a normal project though. Anyway, about the repeating task, I don't see that it works in case I restart the device, even though setPersisted is used. – android developer Jul 15 '15 at 07:23
-
@androiddeveloper I will put it in a project in a moment. For me the repeating task is working even after device restart if I use setPersisted(true). Have you tried listing scheduled tasks using `adb shell dumpsys activity service GcmService --endpoints DataUpdateService` – filipproch Jul 15 '15 at 07:44
-
@jacktech24 Not sure what to look there. I see "Active tasks:none." and also "Pending:" (with nothing after that) . Seems like it's totally empty. Tested on Nexus 4 with stock Kitkat. – android developer Jul 15 '15 at 07:51
-
I've found the cause: I need this permission: android.Manifest.permission.RECEIVE_BOOT_COMPLETED . docs : https://developers.google.com/android/reference/com/google/android/gms/gcm/Task.Builder.html#setPersisted(boolean) – android developer Jul 15 '15 at 08:06
-
@androiddeveloper yeah of course that's a necessary permission, I'am posting the example project in few moments to my github – filipproch Jul 15 '15 at 08:25
-
@jacktech24 Say, if onInitializeTasks is called, the tasks are removed (according to the docs: https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmTaskService.html#onInitializeTasks() ) . How can I prevent it? Or at least re-schedule all the previous tasks ? – android developer Jul 15 '15 at 08:43
-
2@androiddeveloper That's why onInitializeTasks is called, you have to re-schedule your task there. This method is only called upon update to new app version or reinstall. Unfortunatelly there is no easy way to just re-schedule all tasks that were scheduled previously, you have to figure this yourself. – filipproch Jul 15 '15 at 08:45
-
@jacktech24 OK, since I have a lot of questions about this, I've posted a new thread: http://stackoverflow.com/q/31426052/878126 . – android developer Jul 15 '15 at 09:08
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83326/discussion-between-jacktech24-and-android-developer). – filipproch Jul 15 '15 at 12:47
-
13As of I/O 2016, GCMNetworkManager is now Firebase JobDispatcher (https://github.com/firebase/firebase-jobdispatcher-android). – Erik Browne Jun 21 '16 at 23:18
-
@ErikBrowne Quote: `With the launch of Android JetPack’s WorkManager at Google I/O 2018 and its upcoming graduation to beta, we recommend looking at WorkManager for new projects and to submit your feature requests for that offering. Firebase Job Dispatcher will not be compatible with apps targeting releases beyond Android Q. Moving forward, we will focus our investments and efforts towards improving WorkManager.` - https://developer.android.com/topic/libraries/architecture/workmanager/ – Vadim Kotov Feb 04 '19 at 12:50
-
WorkManager executes background task only 10 minutes. GCM Getting deprecated in april 2019. So they recommend to use FCM (Firebase Cloud Messaging). Background service has execution limits starting Android 8 and higher. – Hayk Mkrtchyan Feb 28 '19 at 08:57
There is a very nice and powerful job scheduler library from Evernote. It uses the best method on each SDK level and gives you one very clean and elegant interface to schedule and run jobs.

- 14,917
- 2
- 69
- 74
-
They now recommend using WorkManager, and are planning to deprecate their library in favour of it in some point in future - https://developer.android.com/topic/libraries/architecture/workmanager/ – Vadim Kotov Feb 04 '19 at 12:48
The recommended approach for pre-lollipop devices is to use the https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-
Read more on https://developer.android.com/topic/performance/scheduling.html#fjd
Firebase JobDispatcher is an open-source library that provides an API similar to JobScheduler in the Android platform. Firebase JobDispatcher serves as a JobScheduler-compatibility layer for apps targeting versions of Android lower than 5.0 (API level 21).
Firebase JobDispatcher supports the use of Google Play services as an implementation for dispatching (running) jobs, but the library also allows you to define and use other implementations: For example, you might decide to use JobScheduler or write your own, custom code. Because of this versatility, we recommend that you use this Firebase JobDispatcher if your app targets a version of Android lower than 5.0 (API level 21).
For more information about Firebase JobDispatcher, refer to its documentation and source code.

- 266
- 4
- 12
There are a few methods of running "jobs" pre-lollipop.
As mentioned you can use the JobSchedulerCompat
library, but this library has a huge difference compared to the default Lollipop API:
On Lollipop jobs are batched and scheduled for all apps at once. The library however does not have access to other apps data and therefore can't combine jobs from two different apps using the library.
Another option you might want to try is the AlarmManager
API. This API can be used for scheduling jobs, but the one difference is that the AlarmManager
does not have constraints other than timing for running jobs.
Since KitKat the AlarmManager API also batches jobs when not scheduled with the "exact" methods. Before KitKat jobs are not batched.
If your goal is to sync data, than the Sync-Adapter API might be useful: Sync-Adapter
References: AlarmManager, JobSchedulerCompat

- 8,611
- 6
- 47
- 72
According to the latest background job scheduling APIs, you should use WorkManager.
WorkManager allows you to schedule background tasks that need guaranteed completion (regardless of whether the app process is around or not). WorkManager provides JobScheduler-like capabilities to API 14+ devices, even those without Google Play Services.
WorkManager is queryable (observable), has strong support for graphs of work, and a fluent API.
If you are using JobScheduler, FireBaseJobScheduler, and/or AlarmManager plus BroadcastReceivers, you should consider using WorkManager instead. To learn more, see Work Manager.
You can take a look at the JobSchedulerCompat library.
However, as the authors points out, there's a reason why JobScheduler is only available from Lollipop and onwards, so pay attention to that. Quoting:
However, this library has not been well-tested, so I would advise not using in production at this time. There are no guarantees that this will not run down your battery or cause your device to explode.

- 12,289
- 6
- 47
- 80
Refer this link about Android N Preview API Overview.
GCMNetworkManager, part of Google Play Services, which offers similar job scheduling with compatibility across legacy versions of Android.

- 4,567
- 4
- 29
- 36