The following bit of code, from my understanding, is supposed to set a periodic job that only runs if there is a network connectivity. Meaning that despite being periodic, it won't run unless the required condition is met.
But that doesn't work. It always runs after the period no matter if there is a network or not. Am I doing something wrong ?
JobScheduler mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = null;
builder = new JobInfo.Builder(1, new ComponentName(getPackageName(),
MyJobService.class.getName()))
.setPersisted(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
.setPeriodic(10 * 1000);
There were some issues with setPeriodic() on Android Version < N (Although I don't have official link to issue) but using setMinimumLatency() on older versions did the trick for me.
You can check this SO for further details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With