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

3 回答 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

于 2017-02-06T05:24:43.993 回答
0

试试setPersisted(boolean)方法。

于 2019-06-14T10:02:57.677 回答
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);
    }
于 2017-11-29T17:43:53.827 回答