Keval
Keval

Reputation: 1602

FusedLocationClient doesn't stop searching for gps after request

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.

Upvotes: 4

Views: 1808

Answers (1)

Keval
Keval

Reputation: 1602

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.

Upvotes: 3

Related Questions