我创建了简单的类来使用谷歌的 FusedLocationProviderClient 获取更新的位置。IE:
public class LocationProvider extends LocationCallback {...
private static FusedLocationProviderClient mFusedLocationClient;
private LocationRequest mLocationRequest;
if(mFusedLocationClient == null) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(mContext);
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
locationCallback,
null /* Looper */);
}..}
现在,我使用 AlarmManager 在后台获取更新位置。但是,我无法删除位置更新:
public void stopLocationRequest(){
if(mFusedLocationClient != null){
mFusedLocationClient.removeLocationUpdates(locationCallback);
mFusedLocationClient = null;
mLocationRequest = null;
}
}
private LocationCallback locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location location = locationResult.getLastLocation();
//Code of Generate Notification
}};
现在,我从 BroadcastReceiver 调用上面的stopLocationRequest函数,该函数在 Notification.Builder 中注册为 addAction()。但我收到错误:
A/UnregisterListenerTask: Received call to unregister a listener without a matching registration call.
java.lang.Exception
at com.google.android.gms.internal.zzbar.zzb(Unknown Source)
at com.google.android.gms.internal.zzban.zza(Unknown Source)
at com.google.android.gms.internal.zzbdd.zzb(Unknown Source)
at com.google.android.gms.internal.zzbdd.zza(Unknown Source)
at com.google.android.gms.internal.zzbdb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
而且,我仍然得到更新的位置。这不是删除位置更新。请给我关于这个问题的任何建议。