0

 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(true)
                // don't persist past a device reboot
                .setLifetime(Lifetime.FOREVER)
                // 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_LINEAR)
                // 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);

I am trying to use firebase jobdispatcher to support older devices and newer devices for my calendar app, i am referring this link to setup firebase code in my app is there anything needed to schedule job at particular time at daily weekly etc. or there any other alternative for that? need help in trigger window setting to schedule event at particular time and date recursively. eg daily 4 pm event for 1 week

Swapnil
  • 654
  • 7
  • 27

1 Answers1

0
  final int periodicity = (int)TimeUnit.HOURS.toSeconds(12); // Every 12 hours periodicity expressed as seconds
        final int toleranceInterval = (int) TimeUnit.HOURS.toSeconds(1); // a small(ish) window of time when triggering is OK



         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(true)
                        // persist past a device reboot
                        .setLifetime(Lifetime.FOREVER)
                 .setTrigger(Trigger.executionWindow(periodicity, periodicity + toleranceInterval))
                        // don't overwrite an existing job with the same tag
                        .setReplaceCurrent(false)
                        // retry with exponential backoff
                        .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                        // 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);

    add this line in menifiest file
    <service
      android:name="your service name"
      android:permission="android.permission.BIND_JOB_SERVICE"
      android:exported="true"/>

Calculate the time using as per your requirement and set the periodicity and toleranceInterval

int day = (int)TimeUnit.SECONDS.toDays(seconds);        
 long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
 long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
 long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);

1: Day calculation is correct does not require explanation.
2: TimeUnit.SECONDS.toHours(seconds) will give you direct conversion from seconds to hours without consideration for days you have already calculated. Minus the hours for days you already got i.e, day*24. You now got remaining hours.
3: Same for minute and second. You need to minus the already got hour and minutes respectively.
rehman_00001
  • 1,299
  • 1
  • 15
  • 28
param
  • 393
  • 6
  • 17
  • how to specify 4 pm at specific date or need to calculate that execution window? – Swapnil Sep 26 '17 at 07:23
  • we can't guarantee any jobs will run by a certain point that's dependent on whether the job's runtime constraints are satisfied, as well as how busy the system is. – param Sep 26 '17 at 07:29
  • I think you can use the alarm manger to schedule the job at 4 pm using job dispatcher If you want really strict scheduling - use AlarmManager.setAndAllowWhileIdle – param Sep 26 '17 at 07:41