JobSchedulerCompat

Introduction: Android library to schedule background jobs using JobScheduler, GCMNetworkManager or AlarmManager, depending on the context.
More: Author   ReportBugs   
Tags:

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.
JobInfo JobInfo JobInfo#setClipData(ClipData, int)) and related APIs.
JobService JobService None.
JobParameters JobParameters JobParameters#getClipData()) and related APIs.
PersistableBundle PersistableBundle None.

Schedulers

For each job, the best possible scheduler is used:

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 or 26 No. Yes. Yes.
Firebase JobDispatcher 9 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

Copyright 2017 Doist

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea