0

I'm using firebase job dispatcher and I'm programming a simple periodic job that should run every minute or so when there is internet connection. I know that jobs don't run exactly on time because they wanted to improve the battery life and allow other apps to work as well but the delay is way too long. it can reach 25 minutes which is too long compared to the wanted 1 minute. Is there a way to make the periodic jobs run at least every 3-5 minutes instead ?

Thix is how I program the periodic task:

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

        Job myJob = dispatcher.newJobBuilder()

                // the JobService that will be called
                .setService(MyJobService.class)
                // uniquely identifies the job
                .setTag("my-unique-tag")
                .setRecurring(true)
                .setLifetime(Lifetime.FOREVER)
                // start between 0 and 10 seconds from now
                .setTrigger(Trigger.executionWindow(0, 10))
                .setReplaceCurrent(true)
                .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                // constraints that need to be satisfied for the job to run
                .setConstraints(
                        // only run on an unmetered network
                        Constraint.ON_UNMETERED_NETWORK
                )
                .build();

        dispatcher.mustSchedule(myJob);
student93
  • 307
  • 2
  • 12

2 Answers2

0

If you want your MyJobService to run every minute you need to change this...

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

... to this...

.setTrigger(Trigger.executionWindow(60, 60))

What you had before was telling the FirebaseJobDispatcher to trigger your job every 0-10 seconds. By putting in a window of 60,60 you're telling it to trigger your job approximately every 60 seconds. You could also change this to 55,65 or some other variant but I've found 60,60 works good for me.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
0

I just had the same issue: My Job was scheduled for 60 to 120 seconds but between the first and the second run of my job where like 6 minutes.

I just missed to call jobFinished(JobParameters params, boolean needsReschedule) right after the last line of the last function triggered by the job. Adding this solved the problem in my case.

Hope that helps.