4

我有一个经常性的工作服务Firebase JobDispatcher,它将继续跟踪设备 GPS 位置。如何停止服务,以便应用程序不会跟踪任何活动的按钮单击位置。

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);

单击按钮时,我正在尝试停止服务

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
dispatcher.cancel("gps_tracker");

但该服务继续运行并跟踪位置。

更新

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;
    }


}

现在安排作业

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);

并停止工作

FirebaseJobDispatcher dispatcher = FirebaseSingleton.getInstance(getApplicationContext()).getDispatcher();
dispatcher.cancel("gps_tracker");

它都不起作用。

4

0 回答 0