4

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.

Community
  • 1
  • 1
dharanbro
  • 1,327
  • 4
  • 17
  • 40
  • @ DharanBro i am facing same problem – Hardik Mehta May 03 '17 at 10:37
  • For the time sake i came up with an alternative. The service will run always but the action will executed based on a custom flag that will set true/false in shared preference or sqlite based on the business logic. This might help you but I hope we shall come up with the proper solution asap – dharanbro May 03 '17 at 10:49
  • @ DhanBro can you please give code snippet on how you achieve this? – Hardik Mehta May 03 '17 at 10:57
  • https://stackoverflow.com/questions/51646130/firebasejobdispatcher-onstopjob-not-triggering?noredirect=1#comment90257176_51646130 – Quick learner Aug 02 '18 at 06:22

0 Answers0