android – Firebase JobDispatcher – 计划的作业在设备重启时丢失
作者:互联网
我正在使用Firebase-JobDispatcher.I已经安排了一些工作,如果我保持设备开启,它的工作正常.但是如果我重启我的设备然后预定的工作没有执行或者它没有被重新安排?我使用了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.总是设置一个windowEnd大于windowStart
标签:android,firebase,android-jobscheduler,firebase-job-dispatcher 来源: https://codeday.me/bug/20190627/1308349.html