Firebase Jobdispatcher - Start now AND repeat

Viewed 1259

Per this example, I see that I can have the job start now using Trigger.NOW or a time of 0,0:

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);

My question is:

How do I set the job to start now but also repeat every [time interval] (lets call it 15 minutes)?

1 Answers

To make your job repeat periodically call these two methods:

.setRecurring(true) //true mean repeat it
.setTrigger(Trigger.executionWindow(start, end))

start : is know as windowStart, which is the earliest time (in seconds) the job should be considered eligible to run. Calculated from when the job was scheduled (for new jobs)

end : is know as windowEnd, The latest time (in seconds) the job should be run in an ideal world. Calculated in the same way as windowStart.

Related