Warning
JobSchedulerCompat isn't actively maintained. Consider using WorkManager.
JobSchedulerCompat
JobSchedulerCompat is a backport of JobScheduler for API 16 and above.
Behind the scenes, it relies on JobScheduler, GcmNetworkManager and AlarmManager.
Usage
The API follows JobScheduler's very closely:
PersistableBundle extras = new PersistableBundle();
extras.putString("key", "value");
JobInfo.Builder builder =
new JobInfo.Builder(0, new ComponentName(context, MyJobService.class))
.setMinimumLatency(TimeUnit.MINUTES.toMillis(15))
.setOverrideDeadline(TimeUnit.HOURS.toMillis(2))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setExtras(extras);
JobScheduler.get(context).schedule(builder.build());
This is how MyJobService
could look like:
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
// Spawn a thread to execute your logic.
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
// Stop executing this job.
return false;
}
}
As all regular services, it needs to be declared in your AndroidManifest.xml
:
<service android:name=".MyJobService" />
All necessary components have compatibility variants and follow their counterparts' APIs, with a few limitations:
Component | Modeled after | Limitations |
---|---|---|
JobScheduler |
JobScheduler |
JobScheduler#enqueue(JobInfo, JobWorkItem) and related APIs are unavailable. |
JobInfo |
JobInfo |
JobInfo.Builder#setClipData(ClipData, int) and related APIs are only available on O+.JobInfo.Builder#setRequiredNetwork and related APIs are only available on P+. |
JobService |
JobService |
None. |
JobParameters |
JobParameters |
JobParameters#getClipData() and related APIs are only available on O+.JobParameters#getNetwork() and related APIs are only available on P+. |
PersistableBundle |
PersistableBundle |
None. |
Schedulers
For each job, the best possible scheduler is used:
- JobScheduler (API 21 and above)
- GcmNetworkManager (API 20 and below, if Google Play Services is available)
- AlarmManager (API 20 and below, if Google Play Services is unavailable)
Whenever a job relies on unsupported APIs, JobSchedulerCompat falls back to the next best scheduler. For example, if your job relies on JobInfo.TriggerContentUri
while running on API 21 (where this workflow didn't exist), GcmNetworkManager will be used instead of JobScheduler
for that particular job.
Why
We wanted a library that offered the core functionality of JobScheduler
all the way back to API 16. We wanted it to handle its context gracefully using the best engine available. We wanted it to not have a hard dependency on Google Play Services. We wanted its API to follow JobScheduler
's API closely, so that it can be easily swapped by changing a few import statements.
We looked at the status quo:
Library | Minimum SDK | Requires Google Play Services | Uses best job scheduling engine for context | Same API as JobScheduler |
---|---|---|---|---|
Framework's JobScheduler | 21, 24, 26 or 28 | No | Yes | Yes |
Firebase JobDispatcher | 14 | Yes | No | No |
Evernote's Android-Job | 14 | No | Yes | No |
While all these libraries are phenomenal, neither met all our requirements, so we built one. Its minimum SDK is 16, it doesn't require Google Play Services, it uses the best job scheduling engine depending on the context, and its API mimics JobScheduler's.
License
JobSchedulerCompat is released under the MIT License.