【问题标题】:Firebase Job Dispatcher not triggering at specified time, date as daily/weekly etcFirebase Job Dispatcher 未在指定时间触发,日期为每天/每周等
【发布时间】:2017-09-26 07:00:00
【问题描述】:

 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 来支持我的日历应用程序的旧设备和新设备,我指的是 this link to setup firebase code in my app 是否需要在每周等特定时间安排工作,或者有任何其他替代方案? 在触发窗口设置方面需要帮助以递归地在特定时间和日期安排事件。例如,为期 1 周的每日下午 4 点活动

【问题讨论】:

  • 请出示你写的代码!!
  • @param 检查一次,我添加了我的代码 sn-p

标签: android alarmmanager android-jobscheduler firebase-job-dispatcher


【解决方案1】:
  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.

【讨论】:

  • 如何指定特定日期下午 4 点或需要计算该执行窗口?
  • 我们不能保证任何作业都会运行到某个时间点,这取决于作业的运行时约束是否得到满足,以及系统的繁忙程度。
  • 我认为您可以使用警报管理器使用作业调度程序在下午 4 点安排作业如果您想要真正严格的安排 - 使用 AlarmManager.setAndAllowWhileIdle
猜你喜欢
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多