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
orjobFinished(job, true);
inside the service ? - Sometimes,
setRecurring(boolean)
is set tofalse
, as well asjobFinished(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