Skip to content

Integration with JobScheduler and GcmNetworkManager

Yigit Boyar edited this page May 28, 2016 · 6 revisions

JobManager allows you to run your Jobs under certain circumstances but it only work if your application is currently running. So if you have a network requiring job but your application already died, even if the network is available, the Job is not run until the application is restarted.

To close this gap, JobManager can work with JobScheduler or GcmNetworkManager. The best part, you don't need to care about it. You still create Jobs in the JobManager and the JobManager will make necessary calls to the scheduler API to wake up the application when necessary conditions are present.

Because JobManager is not singleton, it cannot provide this functionality without your help. There are just a few easy steps to enable this integration.

Scheduler API

JobManager V2 introduces a Scheduler API which is used to communicate with external scheduling libraries. It also provides the implementations for the framework's JobScheduler and GCMNetworkManager. If your application is distributed in environments where both of these are unavailable, you can write your own.

Integrations

Integrating with JobScheduler

  1. Create a service that extends FrameworkJobSchedulerService.

    public class MyJobService extends FrameworkJobSchedulerService {
        @NonNull
        @Override
        protected JobManager getJobManager() {
            return ...;// return your JobManager instance
        }
    }
  2. When configuring the JobManager, create a scheduler for your service and set it as the scheduler.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      builder.scheduler(FrameworkJobSchedulerService.createSchedulerFor(this,
                    MyJobService.class));
    }
  3. Register this service in your manifest.

    <service android:name=".services.MyJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />`
  4. Add RECEIVE_BOOT_COMPLETED permission so that your application can be waken up by the scheduler even after the device is reboot.

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Done. Now when you create a persistent job, JobManager will make necessary calls to ensure that your job is run when necessary conditions are met.

Integrating with GcmNetworkManager

JobScheduler is great but it is available only on Lollipop. JobManager can also work with GCMNetworkManager which support API Level 9.

  1. Create a service that extends GcmJobSchedulerService

    public class MyGcmJobService extends GcmJobSchedulerService {
        @NonNull
        @Override
        protected JobManager getJobManager() {
            return ...;// return your JobManager instance
        }
    }
  2. When configuring the JobManager, create a scheduler for your service and set it as the scheduler.

    int enableGcm = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(appContext);
    if (enableGcm == ConnectionResult.SUCCESS) {
      builder.scheduler(GcmJobSchedulerService.createSchedulerFor(this,
                        MyGcmJobService.class), false);
    }
  3. Declare the service in your manifest

    <service
            android:name=".services.MyGcmJobService"
            android:exported="true"
            android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
            <intent-filter>
                <action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
            </intent-filter>
        </service>
  4. Add RECEIVE_BOOT_COMPLETED permission so that your application can be waken up by the scheduler even after the device is reboot.

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  5. Add a dependency on Gcm in your gradle file. JobManager does not depend on GCM since it is a Google service and not available on all devices.

    compile 'com.google.android.gms:play-services-gcm:8.4.0'

Done. Now when you create a persistent job, JobManager will make necessary calls to ensure that your job is run when necessary conditions are met.

Batching

JobManager tries to minimize number of calls to the scheduler. It achieves this by batching requests based on their time requirements. By default, this batching logic divides jobs into 15 minute groups. Check the api docs for Configuration#scheduler to see how you can control this logic.