2

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.

immodi
  • 607
  • 1
  • 6
  • 21
  • How do you get an instance of `LocationProvider` in `BroadcastReceiver`? – Paresh P. Apr 04 '18 at 06:58
  • I have created static object of FusedLocationProviderClient in LocationProvider class. – immodi Apr 04 '18 at 07:01
  • Alright. How about replacing `Looper` parameter with `Lopper.myLooper()`? which is `null` in your case. – Paresh P. Apr 04 '18 at 07:02
  • The [Looper](https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient) object whose message queue will be used to implement the callback mechanism, or null to make callbacks on the calling thread. – immodi Apr 04 '18 at 07:10

0 Answers0