Last active
October 5, 2018 19:07
-
-
Save OrenBochman/aa4a804ae69d9962987987374b066ba4 to your computer and use it in GitHub Desktop.
Android Scheduling - JobDispatcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Firebase JobDispatcher | |
### 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 process. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.firebase:firebase-jobdispatcher:0.8.5' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.firebase.jobdispatcher.JobParameters; | |
import com.firebase.jobdispatcher.JobService; | |
public class MyJobService extends JobService { | |
@Override | |
public boolean onStartJob(JobParameters job) { | |
// Do some work here | |
return false; // Answers the question: "Is there still work going on?" | |
} | |
@Override | |
public boolean onStopJob(JobParameters job) { | |
return false; // Answers the question: "Should this job be retried?" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service | |
android:exported="false" | |
android:name=".MyJobService"> | |
<intent-filter> | |
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/> | |
</intent-filter> | |
</service> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new dispatcher using the Google Play driver. | |
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Job myJob = dispatcher.newJobBuilder() | |
.setService(MyJobService.class) // the JobService that will be called | |
.setTag("my-unique-tag") // uniquely identifies the job | |
.build(); | |
dispatcher.mustSchedule(myJob); | |
// more complex job | |
Bundle myExtrasBundle = new Bundle(); | |
myExtrasBundle.putString("some_key", "some_value"); | |
Job myJob = dispatcher.newJobBuilder() | |
// the JobService that will be called | |
.setService(MyJobService.class) | |
// uniquely identifies the job | |
.setTag("my-unique-tag") | |
// one-off job | |
.setRecurring(false) | |
// don't persist past a device reboot | |
.setLifetime(Lifetime.UNTIL_NEXT_BOOT) | |
// start between 0 and 60 seconds from now | |
.setTrigger(Trigger.executionWindow(0, 60)) | |
// don't overwrite an existing job with the same tag | |
.setReplaceCurrent(false) | |
// retry with exponential backoff | |
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) | |
// constraints that need to be satisfied for the job to run | |
.setConstraints( | |
// only run on an unmetered network | |
Constraint.ON_UNMETERED_NETWORK, | |
// only run when the device is charging | |
Constraint.DEVICE_CHARGING | |
) | |
.setExtras(myExtrasBundle) | |
.build(); | |
dispatcher.mustSchedule(myJob); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cancell one job | |
dispatcher.cancel("my-unique-tag"); | |
//cancell all jobs | |
dispatcher.cancelAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment