I'm using firebase Job Dispatcher to make a periodic job that is triggered by available network. The problem is that the service runs for about 5 minutes sometimes even less and then it stops completely. I tried connecting and disconnecting to the network but the result is the same.
From what I gathered before, the following code is supposed to trigger a periodic task if there is an available network (wifi), the period isn't exact because of the optimisation in the background services but it should still be triggered if I'm constantly connected. But it doesn't, I'm still connected to wifi for hours and working on the phone but the service doesn't run for more than 5 minutes.
This is how I implemented the code following the official docs
I've tried adding both compile 'com.firebase:firebase-jobdispatcher:0.5.2'
and
compile 'com.firebase:firebase-jobdispatcher-with-gcm-dep:0.5.2'
(not at the same time obiously) in gradle file.
JobService
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
public class MyJobService extends JobService {
@Override
public boolean onStartJob(JobParameters job) {
// Do some work here
Toast.makeText(this, "The service is triggered now!",
Toast.LENGTH_LONG).show();
return false; // Answers the question: "Is there still work going on?"
}
@Override
public boolean onStopJob(JobParameters job) {
return false; // Answers the question: "Should this job be retried?"
}
}
manifest
<service
android:exported="false"
android:name=".MyJobService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
and I've added this to my activity:
// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
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")
// this is a periodic job
.setRecurring(true)
// persist after device reboot
.setLifetime(Lifetime.FOREVER)
// start between 0 and 10 seconds from now
.setTrigger(Trigger.executionWindow(0, 10))
// don't overwrite an existing job with the same tag
.setReplaceCurrent(false)
// retry with exponential backoff
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
// constraints that need to be satisfied for the job to run
.setConstraints(
// only run on an unmetered network
Constraint.ON_UNMETERED_NETWORK,
)
.setExtras(myExtrasBundle)
.build();
dispatcher.mustSchedule(myJob);
and I've added this too to the manifest in order to persist the job:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />