FusedLocationAPI can not remove location updates through AlarmManager

大兔子大兔子 提交于 2019-12-08 05:57:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!