I am a little unsure how FirebaseJobDispatcher
(JobScheduler) is suppose to work. What I want it for when a user loses internet connection then gets connectivity again for my app to run and do a sync to check for content updates upon regaining connection.
I know we should not be using Connectivity change broadcast listener and use JobScheduler but it seems that JobScheduler is more of a smarter AlarmManager where it will run even if there was no connectivity change (which I don't need).
Is this the case or am I misunderstanding how it works? If not is there something that will only fire when the user regains internet connection?
JobScheduler is a great option when you want to trigger some actions that just happens when some preconditions are made (Connectivity, Battery and System's Broadcasts). In you case schedule some work that only happens when the user has internet connection.
You can use JobScheduler minimum API 21 and Google Play Service is NOT required. FirebaseJobDispatcher minimum API 9 and Play Service is REQUIRED.
Additionally AndroidJob is a library that has minimum API 14 and does NOT require Play Service.
This video can help to clarify some doubts with FirebaseJobDispatcher and additionally this post by Evernote is good resource.
Below code will trigger on any Network in simple words Wifi or Data Network
Job myJob = mDispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag(JOB_TAG)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(5, 5))
.setLifetime(Lifetime.FOREVER)
.setConstraints(Constraint.ON_ANY_NETWORK)
.setReplaceCurrent(false)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.build();
mDispatcher.schedule(myJob);
Change to .setConstraints(Constraint.ON_UNMETERED_NETWORK) it will work only in wifi network.
Remove the constarints it will work even without network
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