-
Notifications
You must be signed in to change notification settings - Fork 395
Integration with JobScheduler and GcmNetworkManager
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.
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.
-
When configuring the JobManager, create an instance of
FrameworkScheduler
and set it in your configuration. You also need to keep a reference to this scheduler which will be necessary to later on.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { frameworkScheduler = new FrameworkScheduler(MyJobService.class); configurationBuilder.scheduler(frameworkScheduler); }
-
Create a service that extends
FrameworkJobSchedulerService
. In that service, you'll have to override thegetScheduler
method and return the scheduler that you've created when configuring the JobManager.public class MyJobService extends FrameworkJobSchedulerService { @Override public FrameworkScheduler getScheduler() { return MyApp.getInstance().getFrameworkScheduler(); } }
-
Register this service in your manifest.
<service android:name=".services.MyJobService" android:permission="android.permission.BIND_JOB_SERVICE" />`
-
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.
JobScheduler is great but it is available only on Lollipop. JobManager can also work with GCMNetworkManager
which support API Level 9.
-
Create an instance of
GcmScheduler
, keep a reference to it.int enableGcm = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(appContext); if (enableGcm == ConnectionResult.SUCCESS) { gcmTaskScheduler = new GcmScheduler(this, MyGcmJobService.class); builder.scheduler(gcmScheduler); }
-
Create a service that extends
GcmJobSchedulerService
public class MyGcmJobService extends GcmJobSchedulerService { @Override public GcmScheduler getScheduler() { return MyApp.getInstance().getGcmScheduler(); } }
-
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>
-
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.