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

我正在尝试使用firebase jobdispatcher来支持我的日历应用程序的旧设备和新设备,我指的是这个链接在我的应用程序中设置firebase代码是否需要在每周等特定时间安排工作或其他任何事情替代方案?在触发窗口设置方面需要帮助以递归地在特定时间和日期安排事件。例如,为期 1 周的每日下午 4 点活动

4

1 回答 1

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.
于 2017-09-26T07:18:10.987 回答