FusedLocationClient 在请求后不会停止搜索 gps

FusedLocationClient doesn't stop searching for gps after request

我的 FusedLocationProviderClient 在我调用后没有停止

fusedLocationClient.removeLocationUpdates(locationCallback);

在我手动终止服务之前,GPS 定位图标会无限期地显示在通知栏中。

我也在 onDestroy() 方法中调用 stopLocationUpdates。

要启动它,我打电话给:

fusedLocationClient.requestLocatonUpdates(locationRequest, locationCallback, null);

locationCallback 为:

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

这里是stopLocationUpdates():

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

}

我不明白为什么它不停下来。

感谢任何帮助。

这是我所做的:

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() 它会自行处理,然后销毁所有内容。