Android – firebase jobdispatcher – scheduled jobs are lost on device restart
•
Android
I'm using firebase jobdispatcher. I have scheduled some work. If I keep the device on, it works normally. But if I restart my device and the scheduled work is not executed or it is not rescheduled? I used setlifetime (lifetime. Forever). Jobs are still lost when the device is restarted. The following code is used –
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();
resolvent:
After setting lifetime.forever, you add the following permissions to the androidmanifest.xml file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
The following is the code for scheduling work
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);
}
}
Another thing to check is not to set the execution window to 0,0. Always set a windowsend greater than windowstart
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码