I have a recurring job service using Firebase JobDispatcher
which will keep tracking the device GPS location. How can I stop the service so that the application will not track the location on a button click from any activity.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
Job myJob = dispatcher.newJobBuilder()
.setService(GpsTracker.class)
.setTag("gps_tracker")
.setReplaceCurrent(true)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(5,20))
.build();
dispatcher.mustSchedule(myJob);
On a button click I am trying to stop the service
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
dispatcher.cancel("gps_tracker");
But the service continues to run and it tracks the location.
update
public class FirebaseSingleton {
private static FirebaseSingleton mFirebaseSingleton;
private static Context mContext;
private static FirebaseJobDispatcher dispatcher;
private FirebaseSingleton(Context context){
mContext=context;
}
public static synchronized FirebaseSingleton getInstance(Context context) {
if (mFirebaseSingleton == null) {
mFirebaseSingleton = new FirebaseSingleton(context);
}
return mFirebaseSingleton;
}
public static FirebaseJobDispatcher getDispatcher() {
if (dispatcher == null) {
dispatcher= new FirebaseJobDispatcher(new GooglePlayDriver(mContext));
}
return dispatcher;
}
}
Now scheduling the job with
FirebaseJobDispatcher dispatcher = FirebaseSingleton.getInstance(getApplicationContext()).getDispatcher();
Job myJob = dispatcher.newJobBuilder()
.setService(GpsTracker.class)
.setTag("gps_tracker")
.setReplaceCurrent(true)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(5,20))
.build();
dispatcher.mustSchedule(myJob);
And stopping the job with
FirebaseJobDispatcher dispatcher = FirebaseSingleton.getInstance(getApplicationContext()).getDispatcher();
dispatcher.cancel("gps_tracker");
Neither it works.