4

My FusedLocationProviderClient is not stopping after I call

fusedLocationClient.removeLocationUpdates(locationCallback);

The GPS location icon shows indefinitely in the notification bar until I manually terminate the service.

I am calling stopLocationUpdates in the onDestroy() method as well.

To start it, I am calling:

fusedLocationClient.requestLocatonUpdates(locationRequest, locationCallback, null);

locationCallback being:

locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            Location lastLocation = null;

            for (Location location : locationResult.getLocations()) {

                if (lastLocation != null) {
                    if (location.getTime() < lastLocation.getTime()) {
                        lastLocation = location;
                    }
                } else {
                    lastLocation = location;
                }
            }
            if (lastLocation != null) {
                String msg1 = "Best location: http://www.google.com/maps/search/?api=1&query=" + lastLocation.getLatitude() + "%2C" + lastLocation.getLongitude();
                sendSms(lastReceivedNumber, msg1);
            }

            stopLocationUpdates();
        }
    };

Here is stopLocationUpdates():

private void stopLocationUpdates() {
    if (fusedLocationClient != null) {
        fusedLocationClient.removeLocationUpdates(locationCallback);
    }
    fusedLocationClient = null;

}

I can't understand why it doesn't stop.

Any help appreciated.

1
  • 1
    1.) make sure you're not making redundant calls to startLocationUpdates() that may create duplicate listeners 2.) removeLocationUpdates returns a Task, which allows you check if the call was successful 3.) I don't think calling stopLocationUpdates() inside the LocationCallback is a good idea.
    – lasec0203
    Sep 4, 2019 at 9:16

1 Answer 1

3

Here is what I did:

private void startLocationUpdates() {
    fusedLocationClient = new FusedLocationProviderClient(this);
    locationRequest = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            Location lastLocation = null;

            for (Location location : locationResult.getLocations()) {

                if (lastLocation != null) {
                    if (location.getTime() < lastLocation.getTime()) {
                        lastLocation = location;
                    }
                } else {
                    lastLocation = location;
                }
            }
            if (lastLocation != null) {
                String msg1 = "Best location: http://www.google.com/maps/search/?api=1&query=" + lastLocation.getLatitude() + "%2C" + lastLocation.getLongitude();
                sendSms(lastReceivedNumber, msg1);
            }

            stopLocationUpdates();
        }
    };
    try {
        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}

private void stopLocationUpdates() {
    if (fusedLocationClient != null) {
        fusedLocationClient.removeLocationUpdates(locationCallback);
    }
    fusedLocationClient = null;
    locationRequest = null;
    locationCallback = null;
}

I was only nulling the client, nulling everything seems to solve the issue. Now all i have to do is call startLocationUpdates() and it takes care of itself, destroying everything afterwards.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.