Created
October 7, 2017 08:57
-
-
Save Tarelochkin/94bf3a95467939916eac264d73f6a37a to your computer and use it in GitHub Desktop.
Create and schedule com.firebase.jobdispatcher.Job
This file contains 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
Driver driver = new GooglePlayDriver(context); | |
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver); | |
/* Create the Job to periodically create reminders to drink water */ | |
Job constraintReminderJob = dispatcher.newJobBuilder() | |
/* The Service that will be used to write to preferences */ | |
.setService(WaterReminderFirebaseJobService.class) | |
/* | |
* Set the UNIQUE tag used to identify this Job. | |
*/ | |
.setTag(REMINDER_JOB_TAG) | |
/* | |
* Network constraints on which this Job should run. In this app, we're using the | |
* device charging constraint so that the job only executes if the device is | |
* charging. | |
* | |
* In a normal app, it might be a good idea to include a preference for this, | |
* as different users may have different preferences on when you should be | |
* syncing your application's data. | |
*/ | |
.setConstraints(Constraint.DEVICE_CHARGING) | |
/* | |
* setLifetime sets how long this job should persist. The options are to keep the | |
* Job "forever" or to have it die the next time the device boots up. | |
*/ | |
.setLifetime(Lifetime.FOREVER) | |
/* | |
* We want these reminders to continuously happen, so we tell this Job to recur. | |
*/ | |
.setRecurring(true) | |
/* | |
* We want the reminders to happen every 15 minutes or so. The first argument for | |
* Trigger class's static executionWindow method is the start of the time frame | |
* when the | |
* job should be performed. The second argument is the latest point in time at | |
* which the data should be synced. Please note that this end time is not | |
* guaranteed, but is more of a guideline for FirebaseJobDispatcher to go off of. | |
*/ | |
.setTrigger(Trigger.executionWindow( | |
REMINDER_INTERVAL_SECONDS, | |
REMINDER_INTERVAL_SECONDS + SYNC_FLEXTIME_SECONDS)) | |
/* | |
* If a Job with the tag with provided already exists, this new job will replace | |
* the old one. | |
*/ | |
.setReplaceCurrent(true) | |
/* Once the Job is ready, call the builder's build method to return the Job */ | |
.build(); | |
/* Schedule the Job with the dispatcher */ | |
dispatcher.schedule(constraintReminderJob); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment