Firebase JobDispatcher-Scheduled 作业会在设备重新启动时丢失



>我正在使用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();

设置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。 始终设置大于windowStartwindowEnd

我认为在您的 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);
    }

尝试setPersisted(boolean)方法。

相关内容

  • 没有找到相关文章

最新更新