3

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

Psest328
  • 6,575
  • 11
  • 55
  • 90

1 Answers1

0

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.

Fakher
  • 2,098
  • 3
  • 29
  • 45
  • 1
    I know. But if we have it set to 15 minutes, how to have it start NOW and repeat every 15 minutes? As it is, it'll wait 15 minutes until running the first time – Psest328 Nov 03 '17 at 15:49
  • Make a non recurring Job that start with AlarmManager with executionWindow(0, 0) and after 15 minutes make a a recurring Job with executionWindow(0, 15) – Fakher Nov 03 '17 at 15:52