停止Firebase JobDispatcher服务

时间:2017-03-21 08:04:24

标签: android android-service android-gps firebase-job-dispatcher

我有一个使用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");

它都不起作用。

0 个答案:

没有答案