Reputation: 1709
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)?
Upvotes: 41
Views: 27992
Reputation: 1300
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.
Upvotes: 6
Reputation: 266
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.
Upvotes: 12
Reputation: 4569
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.
Upvotes: 1
Reputation: 15379
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.
Upvotes: 17
Reputation: 725
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
Upvotes: 37
Reputation: 8801
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
Upvotes: 9
Reputation: 12339
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.
Upvotes: 4