1

To make things clear from the start, here is the app desired flow :

precise date with day/hour/minutes is selected -> a new Job is created and scheduled with FirebaseJobDispatcher -> the job is triggered exactly at the right date, and runs periodically (say period is 1-2 sec) a code that will define if the job needs to run again. If it does not need to run again, end it definitely, else, re-run it. Also, the job should have a "duration time" (usually 60sec) that will end it whatever happens in the job.

So far I'm confused about three things :

  • The Trigger window seems completely unreliable and runs the job approximately when it wants
  • Should I use FirebaseJobDispatcher#setRecurring or jobFinished(job, true); inside the service ?
  • Sometimes, setRecurring(boolean) is set to false, as well as jobFinished(job, false);, and the job still runs again

Maybe I should use AlarmManager instead ? But it seemed like the new way was FirebaseJobDispatche, so I don't know.

Here is some code

Service:

@Override
public boolean onStartJob(final JobParameters job) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            simulateBackgroundTask(job);
        }
    }).start();

    return true;
}

private void simulateBackgroundTask(JobParameters job) {
    try {

        Log.d(TAG, "completeJob: " + "jobStarted --------------------");

        //request server or db
        Thread.sleep(2000);

        Log.d(TAG, "completeJob: " + "jobFinished --------------------");

        //shoudRunAgain is from the result of the request
        if(shouldRunAgain)
            jobFinished(job, true);
        else
            jobFinished(job, false);

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}


@Override
public boolean onStopJob(JobParameters job) {
    return false;
}

Activity

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(App.getContext()));

    Job job = dispatcher.newJobBuilder()
            .setLifetime(Lifetime.FOREVER)
            .setService(SearchService.class)
            .setTag("SearchService")
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(startDate,endDate))
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();

    dispatcher.mustSchedule(job);

Any advices or solutions are welcomed, thank you

David Seroussi
  • 1,650
  • 2
  • 17
  • 34
  • Doze or any other battery saving mode make any sort of cron job in Android to be executed solely when a maintenance cycle is started, this will also happen with alarms. At least with the FirebaseJobDispatcher you can add constraints. The problem with using Alarm and the JobScheduler is maintaining to blocks of code for different API levels. – cutiko Nov 07 '17 at 00:10
  • So there are no ways to be sure the task will run exactly at the right time ? – David Seroussi Nov 07 '17 at 00:22
  • No, and that is correct and what Android developers deserve (we all). That is the way to respect the battery of the user. And this happened because Android developers abuse from everything, broadcast receivers, services, alarms. There have being already 2 changes for services and now in Oreo it had tobe restricted even more! Because developers make eternal sticky service for pointless tasks – cutiko Nov 07 '17 at 00:33

0 Answers0