Job Scheduler is working as expected on Android Marshmallow and Lollipop devices, but it is not running and Nexus 5x (Android N Preview).
Code for scheduling the job
ComponentName componentName = new ComponentName(MainActivity.this, TestJobService.class.getName());
JobInfo.Builder builder;
builder = new JobInfo.Builder(JOB_ID, componentName);
builder.setPeriodic(5000);
JobInfo jobInfo;
jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobInfo = builder.build();
int jobId = jobScheduler.schedule(jobInfo);
Service is defined in manifest as:
<service android:name=".TestJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
Is any one having this issue on Android N (Preview)?
I have found the answer to the issue that we are facing with Nougat devices. Nougat devices are not able to schedule the job if the job needs to be re-scheduled lesser than 15 minutes.
I tried out by giving the Interval time as 15 minutes and the job started getting scheduled every 15 minutes.
Job Scheduler Code:
Job Service:
In short if you would have increased the Interval Time to 15 minutes the code would have started working.
private static final long REFRESH_INTERVAL = 15 * 60 * 1000;
If someone is still trying to overcome the situation,
Here is a workaround for >= Android N (If you want to set the periodic job lower than 15 minutes)
Check that only setMinimumLatency is used. Also, If you are running a task that takes a long time, the next job will be scheduled at, Current JOB Finish time + PROVIDED_TIME_INTERVAL
.SetPeriodic(long millis) works well for API Level below Android N
UPDATE: If you are considering your repeating job to run while in Doze Mode and thinking about JobScheduler, FYI: JobSchedulers are not allowed to run in Doze mode.
I have not discussed about the Dozing because we were talking about JobScheduler. Thanks, @Elletlar, for pointing out that some may think that it will run even when the app is in doze mode which is not the case.
For doze mode, AlarmManager still gives the best solution. You can use setExactAndAllowWhileIdle() if you want to run your periodic job at exact time period or use setAndAllowWhileIdle() if you're flexible.
You can also user setAlarmClock() as device always comes out from doze mode for alarm clock and returns to doze mode again. Another way is to use FCM.
In Android Nougat the
setPeriodic(long intervalMillis)
method call makes use ofsetPeriodic (long intervalMillis, long flexMillis)
to schedule periodic jobs.As per the documentation:
Sample periodic job scheduled for 5 seconds:
The above code works well in Lollipop & Marshmallow but when you run in Nougat you will notice the following log:
Since we have set the periodic refresh interval to 5 seconds which is less than the threshold
getMinPeriodMillis()
. Android Nougat enforces thegetMinPeriodMillis()
.As a workaround, I'm using following code to schedule jobs at periodic intervals if job interval is less than 15minutes.
Sample JobService example:
if you want to run the code periodically less than 15 minutes then you can use a tricky way. Set your
jobFinished()
like thisit will reschedule the code with a retry strategy. Define a custom backoff criteria using
in the builder. Then it will run periodically