JobDispatcher-Firebase job downloads are lost when the device is rebooted

I am using Firebase-JobDispatcher . I have scheduled some tasks and its work is excellent if I keep the device turned on. But if I reboot my device and then the scheduled tasks are not executed or it will not be transferred? I have used setLifetime(Lifetime.FOREVER). The remaining jobs are lost when the device is rebooted. Below is the code that uses -

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
source share
3 answers

After setting, Lifetime.FOREVERyou added the following permission to the fileAndroidManifest.xml

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

And below is the code for scheduling a job

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. a windowEnd , windowStart

+3

, 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);
    }
0

Give it a try setPersisted(boolean).

0
source

Source: https://habr.com/ru/post/1668900/


All Articles