I am developing an Android app, and I used the Firebase JobDispatcher library to run a background job every time the user turns on Wi-Fi or mobile data, but unfortunately the library was deprecated and now the new WorkerManager is the way to go.
My question is how to run a job using the WorkerManager every time the internet is available only?
I have checked the WorkerManager library migration guide and documentation, but I couldn't achieve that using OneTimeWorkRequest or PeriodicWorkRequest.
Here is the code I used with the Firebase JobDispatcher library to achieve that.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job networkJob = dispatcher.newJobBuilder()
.setService(NetworkJobService.class)
.setTag(Const.NETWORK_JOB_TAG)
.setReplaceCurrent(true)
.setLifetime(Lifetime.FOREVER)
.setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(0, 0))
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
dispatcher.schedule(networkJob);