4

I am using Firebase-JobDispatcher.I have scheduled some jobs and its working fine if i keep the device switch on.But if i reboot my device then the scheduled jobs doesn't execute or it doesn't get rescheduled?I have used setLifetime(Lifetime.FOREVER).Still jobs are lost on device reboot.Below is the code i m using-

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();
Android Developer
  • 9,157
  • 18
  • 82
  • 139

3 Answers3

3

After setting Lifetime.FOREVER you have add the following permission in AndroidManifest.xml file

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

And below is the code to schedule 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);
  }
}

One more thing to check is not set execution window to 0,0. always set a windowEnd greater that windowStart

arjun
  • 3,514
  • 4
  • 27
  • 48
  • Are the constraints met after reboot? I mean is the phone connected to internet to run the job? – arjun Feb 06 '17 at 05:36
  • yes it is connected to internet and if i schedule new job that job executes.only if i reboot device jobs are lost – Android Developer Feb 06 '17 at 05:36
  • I am testing by disconnecting internet while scheduling jobs and reconnecting to internet after device reboot – Android Developer Feb 06 '17 at 05:40
  • Can you post the jobservice code. Just to know how you are deciding whether job is executed or not – arjun Feb 06 '17 at 05:40
  • I have put a log statement at beginning of onStartJob to check whether its called or not .Also i am sending images to server inside my job service and on basis of these things i can know that my job is executed or not – Android Developer Feb 06 '17 at 05:59
  • i have added my code to schedule the job. it works fine for me even after a reboot. i had the same problem of job not rescheduling after reboot. just adding that permission made it to work. – arjun Feb 06 '17 at 06:06
  • what intent filters are u using for service? – Android Developer Feb 06 '17 at 08:31
  • Im not using any intentfilters – arjun Feb 06 '17 at 09:19
  • but in github documentation its written we have to use `` – Android Developer Feb 06 '17 at 09:55
  • If i increase `Trigger.executionWindow(0,0)` to `Trigger.executionWindow(0, 60)` than it works fine and job gets executed after reboot when network connection is there.So `Lifetime.FOREVER` doesn't work if the execution window end is less or small.i.e.around Trigger.executionWindow(0, 0) to Trigger.executionWindow(0, 15).Please test and update answer before i can accept your answer. – Android Developer Feb 07 '17 at 16:44
  • yes, setting `Trigger.executionWindow(0,0)` is the problem here. I have edited the answer – arjun Feb 09 '17 at 07:11
  • Try with `Trigger.executionWindow(0, 5) ..it will also not execute the job after reboot.As per my understanding,the problem is not that windowEnd and windowStart is same..I think Lifetime.FOREVER doesn't work if the execution window end is less or small..Trigger.executionWindow(0, 0) is used to start the job immediately if all constraints are met – Android Developer Feb 09 '17 at 07:15
  • I have set this .setTrigger(Trigger.executionWindow(Long.valueOf(TimeUnit.HOURS.toSeconds(12)).intValue(), Long.valueOf(TimeUnit.HOURS.toSeconds(12)).intValue() + 60)). I turned off the phone and after 15 hours I turned on, then job is not running. Do you know what happens in this scenario? I haven't found a solution or answer yet. I tried to change the system time and doesn't work either. – Leandro Ocampo Apr 26 '17 at 13:47
0

I think in your MyJobService should return false so that the job can be rescheduled after executing;

  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);
    }
Goodlife
  • 3,822
  • 2
  • 24
  • 23
0

Try setPersisted(boolean) method.

double-beep
  • 5,031
  • 17
  • 33
  • 41
user4057066
  • 295
  • 3
  • 14