收藏    我在用   0 人在用

 GitHub     下载     同步

103    1611    189    2 天前

Firebase JobDispatcher Build Status

The Firebase JobDispatcher is a library for scheduling background jobs in your
Android app. It provides a JobScheduler-compatible API that works on all
recent versions of Android (API level 9+) that have Google Play services


What's a JobScheduler?

The JobScheduler is an Android system service available on API levels 21
(Lollipop)+. It provides an API for scheduling units of work (represented by
JobService subclasses) that will be executed in your app's

Why is this better than background services and listening for system broadcasts?

Running apps in the background is expensive, which is especially harmful when
they're not actively doing work that's important to the user. That problem is
multiplied when those background services are listening for frequently sent
broadcasts ( and
android.hardware.action.NEW_PICTURE are common examples). Even worse, there's
no way of specifying prerequisites for these broadcasts. Listening for
CONNECTIVITY_CHANGE broadcasts does not guarantee that the device has an
active network connection, only that the connection was recently changed.

In recognition of these issues, the Android framework team created the
JobScheduler. This provides developers a simple way of specifying runtime
constraints on their jobs. Available constraints include network
, charging state, and idle

This library uses the scheduling engine inside Google Play
to provide a
backwards compatible (back to Gingerbread) JobScheduler-like API.

This I/O presentation has more information on why background services can be
harmful and what you can do about them:

Android battery and memory optimizations

There's more information on upcoming changes to Android's approach to background
services on the Android developer preview page.


The FirebaseJobDispatcher currently relies on the scheduling component in Google
Play services. Because of that, it won't work on environments without Google
Play services installed.

Comparison to other libraries

Library Minimum API Requires Google Play Service API<sup>1</sup> Custom retry strategies
Framework JobScheduler 21 No JobScheduler Yes
Firebase JobDispatcher 9 Yes JobScheduler Yes
evernote/android-job 14 No<sup>2</sup> Custom Yes

<a name="fn1">1</a>: Refers to the methods that need to be implemented in the
Service subclass.<br>
<a name="fn2">2</a>: Uses AlarmManager to support API levels <= 21 if Google
Play services is unavailable.<br>

Getting started


If you don't have a dependency on, add the following to your
build.gradle's dependencies section:

compile 'com.firebase:firebase-jobdispatcher:0.5.2'

Otherwise add the following:

compile 'com.firebase:firebase-jobdispatcher-with-gcm-dep:0.5.2'

NOTE: These variants are a temporary requirement. Work is ongoing to consolidate
them into the same bulid.


Writing a new JobService

The simplest possible JobService:

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyJobService extends JobService {
    public boolean onStartJob(JobParameters job) {
        // Do some work here

        return false; // Answers the question: "Is there still work going on?"

    public boolean onStopJob(JobParameters job) {
        return false; // Answers the question: "Should this job be retried?"

Adding it to the manifest

        <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>

Creating a Dispatcher

// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

Scheduling a simple job

Job myJob = dispatcher.newJobBuilder()
    .setService(MyJobService.class) // the JobService that will be called
    .setTag("my-unique-tag")        // uniquely identifies the job


Scheduling a more complex job

Bundle myExtrasBundle = new Bundle();
myExtrasBundle.putString("some_key", "some_value");

Job myJob = dispatcher.newJobBuilder()
    // the JobService that will be called
    // uniquely identifies the job
    // one-off job
    // don't persist past a device reboot
    // start between 0 and 60 seconds from now
    .setTrigger(Trigger.executionWindow(0, 60))
    // don't overwrite an existing job with the same tag
    // retry with exponential backoff
    // constraints that need to be satisfied for the job to run
        // only run on an unmetered network
        // only run when the device is charging


Cancelling a job


Cancelling all jobs



Next steps

  • Browse the [API documentation][]

[API documentation]: TODO: put link here



See the file.


This library is actively supported by Google engineers. If you encounter any
problems, please create an issue in our tracker.


Apache, see the LICENSE file.

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册