Firebase JobDispatcher - 设备重启后计划任务丢失

4

我正在使用Firebase-JobDispatcher。我已经安排了一些工作,如果我把设备保持开启状态,那么它的工作正常。但是,如果我重新启动设备,预定的工作将不会执行或者不会被重新安排?我已经使用了setLifetime(Lifetime.FOREVER),但是任务在设备重启后仍然丢失。以下是我使用的代码-

Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("DataSend")
.setRecurring(false)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(0, 0))
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(Constraint.ON_ANY_NETWORK)
.setExtras(myExtrasBundle)
.build();

有一个问题 - https://github.com/firebase/firebase-jobdispatcher-android/issues/101 - Oleksandr
3个回答

3

在设置 Lifetime.FOREVER 后,您需要在 AndroidManifest.xml 文件中添加以下权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

以下是调度作业的代码。
Job job = jobDispatcher.newJobBuilder()
    .setService(MyJobService.class)
    .setTrigger(Trigger.executionWindow(windowStartTime, 3600))
    .setTag(PENDING_AUTH_JOB) //identifier for the job
    .setRecurring(false) // should not recur
    .setLifetime(Lifetime.FOREVER) // should persist device reboot
    .setReplaceCurrent(false) // no need to replace previous job
    .setConstraints(Constraint.ON_ANY_NETWORK) // set network availability constraint
    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
    .build();
try {
  jobDispatcher.mustSchedule(job);
} catch (FirebaseJobDispatcher.ScheduleFailedException e) {
  if (retryCount-- > 0) {
    scheduleJob(0);
  }
}

还有一件需要检查的事情是不要将执行窗口设置为0,0。始终将windowEnd设置为大于windowStart


我没有使用任何意图过滤器。 - arjun
但是在 Github 的文档中写道,我们必须使用 <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/> - Android Developer
如果我将 Trigger.executionWindow(0,0) 增加到 Trigger.executionWindow(0, 60),那么它就可以正常工作,并且在重新启动后当网络连接可用时执行作业。因此,如果执行窗口结束时间较短,例如 Trigger.executionWindow(0, 0) 到 Trigger.executionWindow(0, 15) 左右,则 Lifetime.FOREVER 将无法正常工作。请在我接受您的答案之前进行测试和更新。 - Android Developer
尝试使用Trigger.executionWindow(0, 5),它也不会在重启后执行作业。据我理解,问题不在于windowEnd和windowStart相同。我认为如果执行窗口结束时间较短,则Lifetime.FOREVER无法正常工作。如果所有约束条件都得到满足,可以使用Trigger.executionWindow(0, 0)立即启动作业。 - Android Developer
我已经设置了这个.setTrigger(Trigger.executionWindow(Long.valueOf(TimeUnit.HOURS.toSeconds(12)).intValue(), Long.valueOf(TimeUnit.HOURS.toSeconds(12)).intValue() + 60))。我关闭了手机,15小时后打开,然后任务没有运行。你知道在这种情况下会发生什么吗?我还没有找到解决方案或答案。我尝试更改系统时间,但也不起作用。 - Leandro Ocampo
显示剩余8条评论

0
尝试使用setPersisted(boolean)方法。

0

我认为在你的 MyJobService 中应该返回 false,这样任务执行后可以重新安排调度;

  public boolean onStartJob(final com.firebase.jobdispatcher.JobParameters jobParameters) {

        //Offloading work to a new thread.
        new Thread(new Runnable() {
            @Override
            public void run() {
                realm=Realm.getDefaultInstance();

                codeYouWantToRun(jobParameters);
            }
        }).start();

        return true;
    }



 public void codeYouWantToRun(final JobParameters parameters) {
 Log.d(TAG, "completeJob: " + "jobStarted");
//bla bla super code doing its linga linga ling 
                Log.d(TAG, "completeJob: " + "jobFinished");

                    //Tell the framework that the job has completed and doesnot needs to be reschedule. Set jobFinished false so that it can rescheduled on a change of network
                    jobFinished(parameters, false);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接