我正在尝试使用 firebase 作业调度程序执行后台任务,问题是应用程序将在 kit to oreo 上运行,我想在特定的定义时间每天在后台执行作业。例如,如果我在早上 7 点工作并且当前时间是下午 5 点,那么函数会在两个小时后执行,但我想每天晚上 7 点执行,
FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(new
GooglePlayDriver(context));
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.getDefault());
Calendar fixTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
Locale.getDefault());
fixTime.set(Calendar.HOUR, 7);
fixTime.set(Calendar.MINUTE, 0);
fixTime.set(Calendar.SECOND, 0);
fixTime.set(Calendar.AM_PM, Calendar.PM);
long diff = fixTime.getTimeInMillis() - now.getTimeInMillis() ;
int seconds = (int) Math.abs(TimeUnit.MILLISECONDS.toSeconds(diff));
Job networkJob = firebaseJobDispatcher.newJobBuilder()
.setService(NotificationJobService.class)
.setTag(myJobTag)
.setRecurring(true)
.setTrigger(Trigger.executionWindow( seconds, seconds+40))
.setLifetime(Lifetime.FOREVER)
.setReplaceCurrent(true)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.build();
firebaseJobDispatcher.schedule(networkJob);