7

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?

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Hi @tyczj, we have a same requirement as you. We want to start an alarm broadcast receiver, when internet becomes from unavailable to available. We use to achieve this seamless via `CONNECTIVITY_CHANGE`. But, since listening to `CONNECTIVITY_CHANGE` no longer work, if you kill the app, have you figure out how to use `FirebaseJobDispatcher`? – Cheok Yan Cheng May 21 '17 at 09:59
  • @CheokYanCheng Job dispatcher does not work this way, its a smarter alarm manager. I use push notifications now and when the user gets internet back the push notifications come in and the content updates – tyczj May 22 '17 at 12:33

3 Answers3

3

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.

Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25
2

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

shivam
  • 21
  • 2
0

The TestApp for the JobDispatcher provides pretty good examples; while ordinary, one can use a Builder to create a Job for the Dispatcher. just found this answer here, which explains it in more detail.

/* a recurring job, being triggered in the time-frame in between 59-61 seconds */
Job.Builder builder = dispatcher
    .newJobBuilder()
    .setTag("SomeJob")
    .setService(SomeJobService.class)
    .setRecurring(true)
    .setTrigger(Trigger.executionWindow(59, 61));
Community
  • 1
  • 1
Martin Zeitler
  • 1
  • 19
  • 155
  • 216