火力地堡工作分派不触发在指定的时间,日期,每日/每周等

问题描述:

火力地堡工作分派不触发在指定的时间,日期,每日/每周等

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

我试图用火力jobdispatcher支持旧设备和新设备为我的日历应用程序,我指this link to setup firebase code in my app有什么需要安排在特定的时间在每日每周的工作等,或有任何其他的替代方案吗? 需要触发窗口设置的帮助才能在特定的时间和日期递归地安排事件。例如每天下午4点活动一周

+1

请显示代码你写的东西! – param

+0

@param检查一次,我加了我的代码片段 – Swapnil

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) 
         // don't 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. 
+0

如何在特定日期指定4点或需要计算该执行窗口? – Swapnil

+0

我们不能保证任何作业都会依赖于作业的运行时限制是否得到满足,以及系统的繁忙程度。 – param

+0

我认为您可以使用闹钟管理器在下午4点使用求职调度员安排工作 如果您想要非常严格的计划 - 使用AlarmManager.setAndAllowWhileIdle – param