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.

4

1 回答 1

3

这是我所做的:

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;
}

我只是让客户无效,让一切无效似乎可以解决问题。现在我所要做的就是打电话startLocationUpdates(),它会照顾好自己,然后摧毁一切。

于 2018-02-14T19:45:57.227 回答