我有一个使用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");
它都不起作用。