Android 在特定时间触发Firebase作业调度程序
我想在特定时间触发Firebase作业调度程序,如Android 在特定时间触发Firebase作业调度程序,android,job-scheduling,Android,Job Scheduling,我想在特定时间触发Firebase作业调度程序,如1498383000000L。但是触发器方法有一个窗口,我们在其中指定时间。我如何克服这个问题 Job newJob = dispatcher.newJobBuilder() //update if any task with the given tag exists. .setReplaceCurrent(true) //Integrate the job you w
1498383000000L
。但是触发器方法有一个窗口,我们在其中指定时间。我如何克服这个问题
Job newJob = dispatcher.newJobBuilder()
//update if any task with the given tag exists.
.setReplaceCurrent(true)
//Integrate the job you want to start.
.setService(ScheduledJobService.class)
.setTag("UniqueTagForYourJob")
// Run between 30 - 60 seconds from now.
.setTrigger(Trigger.executionWindow(30, 60))
.build();
return newJob;
您可以计算未来特定时间与现在特定时间之间的差值(以秒为单位),并将其设置为
Trigger.executionWindow()
方法中的windowStart
参数。您可以设置与windowEnd
参数相同的值,以使作业开始时间尽可能接近该时间,但即使在这种情况下,也不能保证作业在该时间开始
根据我的经验,当windowStart
和windowEnd
值相同时,作业通常在windowStart
时间后不超过一分钟(有时几秒钟)开始
在下面的示例中,根据所需的作业启动时间计算启动时间,并为作业启动设置60秒窗口
long timeInFuture = 1498383000000L;
int jobTime = Math.round((System.currentTimeMillis() - timeInFuture) / 1000);
Job newJob = null;
if (jobTime >= 0){
newJob = dispatcher.newJobBuilder()
.setReplaceCurrent(true)
.setService(ScheduledJobService.class)
.setTag("UniqueTagForYourJob")
.setTrigger(Trigger.executionWindow(jobTime, jobTime + 60))
.build();
}
return newJob;