Scheduling Jobs with JobScheduler in Android

lucasb.aquino picture lucasb.aquino · Mar 18, 2015 · Viewed 7.3k times · Source

I have a problem with jobs schedules with JobScheduler in new Android API 21. This is the code what I schedule the job with 60 seconds interval like below:

ComponentName serviceName = new ComponentName(this, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(0, serviceName)
        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
        .setPeriodic(60000)
        .build();

My JobService only print the time of runtime in Logcat, but the log show that service run in this moments:

03-18 08:37:26.334: I/JOB(32662): Wed Mar 18 08:37:26 BRT 2015
03-18 08:37:56.364: I/JOB(32662): Wed Mar 18 08:37:56 BRT 2015
03-18 08:39:21.418: I/JOB(32662): Wed Mar 18 08:39:21 BRT 2015
03-18 08:41:51.670: I/JOB(32662): Wed Mar 18 08:41:51 BRT 2015
03-18 08:45:52.192: I/JOB(32662): Wed Mar 18 08:45:52 BRT 2015
03-18 08:54:20.678: I/JOB(32662): Wed Mar 18 08:54:20 BRT 2015

It's strange because the Job it should execute at least 1 time within 1 minute as I set with setPeriodic(60000) method. It is also curious how the interval increases between runs. At this moment the time is Wed Mar 18 09:09:00 BRT 2015 and the Job don't be executed more.

Is it a problem with JobScheduler API? (I'm running in Nexus 5 with Android 5.0.1)

Answer

MinceMan picture MinceMan · Apr 13, 2015

The time changing has to do with the Back-off Criteria for retrying jobs. By default it is set to exponential. I'm guessing that your also not correctly finishing your job when your done with it by calling jobFinished(JobParameters params, boolean needsReschedule).

I wrote a blog post which focus' on all of the little things with the JobScheduler. I highly recommend reading it.