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.