Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setRequiredNetworkType doesn't work on periodic tasks?

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);
like image 874
student93 Avatar asked Mar 17 '26 11:03

student93


1 Answers

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.

like image 185
Sagar Avatar answered Mar 19 '26 23:03

Sagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!