Shaunak Srivastava
Shaunak Srivastava

Reputation: 63

Location call backs not stopping after calling removeLocationUpdates

I am receiving location update through this class everything working fine, except sometimes location callbacks don't stop after calling StopLocationUpdate(). Please help me to rectify the bug. I have checked answers available to stop location updates and i have implemented the same.

FusedLocationProvider Class:

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;

class FusedLocationProvider {

    /*Fused API Client Objects.*/
    private FusedLocationProviderClient mFusedLocationClient;
    private LocationCallback mLocationCallback;
    private LocationRequest mLocationRequest;


    /*Listener Interface.*/
    private FusedLocationListener iFusedLocationListener;


    /*Source activity.*/
    private Context context;
    private boolean isRequestingUpdates = false;


    FusedLocationProvider(Context context, LocationRequest locationRequest) {

        this.context = context;
        mLocationRequest = locationRequest;

        /*Get FusedLocationProviderClient.*/
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this.context);

        /*Start process of building the LocationCallback, LocationRequest.*/
        createLocationRequestCallback();
    }

    void StartLocationUpdate(FusedLocationListener iListener) {

        /*Hook the listener for callbacks.*/
        iFusedLocationListener = iListener;

        /*Check if request already ongoing.*/
        if (!isRequestingUpdates) {

            initiateLocationUpdate();
        }
    }

    void StopLocationUpdate() {

        if (isRequestingUpdates) {

            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
            iFusedLocationListener.OnLocationUpdateStopped();
            isRequestingUpdates = false;
        }
    }

    private void createLocationRequestCallback() {

        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {

                iFusedLocationListener.OnLocationUpdate(locationResult);
            }
        };
    }


    private void initiateLocationUpdate() {

        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.


            return;
        }

        iFusedLocationListener.OnLocationUpdateStarted();
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null /* Looper */);
        isRequestingUpdates = true;
    }
}

Upvotes: 5

Views: 783

Answers (1)

jc12
jc12

Reputation: 1829

I had a similar problem. The issue turned out to be that I had multiple instances of the class that held the locationCallback so the instance that was called to stop the location updates was different than the instance that started them.

So for your code, do you maybe have multiple instances of FusedLocationProvider?

Upvotes: 3

Related Questions