0

I'm using the following code to make a job run every time there is available internet. What I want in addition is, after the service is triggered (due to available connection) I want that service to continue running g periodically (every 30 seconds) as long as there is internet and then when the connectivity is no longer available, the service should stop and only resume the next time there is internet.

FirebaseJobDispatcher jobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(MainActivity.this));

                        .setTag("JobService")

                        .setRecurring(true)

                        .setLifetime(Lifetime.FOREVER)
                        .setService(JobService.class)

                        .setTrigger(Trigger.executionWindow(0,10))

                        .setReplaceCurrent(true)
                        .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL);    

                builder.addConstraint(Constraint.ON_UNMETERED_NETWORK);
                jobDispatcher.mustSchedule(builder.build());

I thought of making the JobService itself schedule the next time it is going to run (in thirty seconds) and after the time is up, test if there is internet then ok else I'll call the Onstop method but it didn't feel like the right approach to solve this.

student93
  • 307
  • 2
  • 12

1 Answers1

1

you can write : setTrigger(Trigger.executionWindow(30,40)) . explanation: firebase job dispatcher github

Scheduling a more complex job

Bundle myExtrasBundle = new Bundle();
myExtrasBundle.putString("some_key", "some_value");

Job myJob = dispatcher.newJobBuilder()
    // the JobService that will be called
    .setService(MyJobService.class)
    // uniquely identifies the job
    .setTag("my-unique-tag")
    // one-off job
    .setRecurring(false)
    // don't persist past a device reboot
    .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
    // start between 0 and 60 seconds from now
    .setTrigger(Trigger.executionWindow(0, 60))

or for executionWindow, the rule is:

.setTrigger(Trigger.executionWindow(
                        INTERVAL_IN_SECONDS,
                        INTERVAL_IN_SECONDS + TIME_WINDOW_IN_SECONDS
                ))

reference: https://stackoverflow.com/a/39909986/1537413

Community
  • 1
  • 1
Dika
  • 2,213
  • 4
  • 33
  • 49
  • I'm sorry but I don't understand how the command that I used: .setTrigger(Trigger.executionWindow(0,10)) is any different from you using .setTrigger(Trigger.executionWindow( INTERVAL_IN_SECONDS, INTERVAL_IN_SECONDS + TIME_WINDOW_IN_SECONDS )) – student93 Mar 25 '17 at 18:35
  • an the one I used is only triggering the job once when it is first connected to internet and the next time would be after disconnecting and connecting again which isn't the behaviour that I want since I want it to be triggered periodically as long as the user is still connected – student93 Mar 25 '17 at 18:36
  • you want it to running periodically every 30 seconds, right? – Dika Mar 26 '17 at 05:18
  • yeah, I tried using setTrigger but the issue is that it works for 3 to 4 times and then it just stops. I've had much better results with jobscheduler but seeing as it only works for api>=21 and since firebase job dispatcher supposedly uses job scheduler for api>=21 and alarm manager to mimic job scheduler behaviour for api<21 I wanted to use it but it stops working after a while. – student93 Mar 26 '17 at 16:16
  • you tried to write ` setTrigger(Trigger.executionWindow(30,40))` ? – Dika Mar 27 '17 at 01:38
  • no I haven't. Whatever interval I choose I always start from 0, (0,10) or (0,30) does it make a difference ? – student93 Mar 27 '17 at 21:38
  • I haven't tried (0,10), but I have tried (30,40) and it works just like you want in your question. I suggest you try (30,40). it will run service 30 seconds after, and will periodically run service per 30 seconds. if you want to run your service immediately too, you can manually trigger the first run of your service. – Dika Mar 29 '17 at 00:01