问题
I have created simple class to get updated location using google's FusedLocationProviderClient. i.e.:
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 */);
}..}
Now, I getting updated location in background using AlarmManager. But, I cannot able to remove location updates:
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
}};
Now, I calling this above stopLocationRequest function from BroadcastReceiver which register with Notification.Builder as addAction().But I getting error:
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)
And, I still getting updated location.It's not remove location updates. Please give me any suggestion of this issue.
来源:https://stackoverflow.com/questions/49644547/fusedlocationapi-can-not-remove-location-updates-through-alarmmanager