JobSchedulerCompat

简介:Android library to schedule background jobs using JobScheduler, GCMNetworkManager or AlarmManager, depending on the context.
更多:作者   提 Bug   
标签:

JobSchedulerCompat is a backport of JobScheduler for API 16 and above. Depending on the context, it will use:

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 None.
JobInfo JobInfo JobInfo.TriggerContentUri and related APIs:
JobInfo#getFlexMillis()) and related APIs:
JobService JobService None.
JobParameters JobParameters JobParameters#getTriggeredContentUris()) and related APIs:
PersistableBundle PersistableBundle None.

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 No. Yes. Yes.
Firebase JobDispatcher 9 Yes. No. Similar.
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 2016 Google, Inc.
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.
Android 开发经验分享
用了一年多的理财推荐
Android 开发经验分享
用了一年多的理财推荐