Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseJobDispatcher run on network change

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?

like image 457
tyczj Avatar asked Apr 18 '17 13:04

tyczj


2 Answers

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.

like image 107
Arturo Mejia Avatar answered Sep 27 '22 20:09

Arturo Mejia


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

like image 39
shivam Avatar answered Sep 27 '22 21:09

shivam