【问题标题】:FusedLocationClient doesn't stop searching for gps after requestFusedLocationClient 在请求后不会停止搜索 gps
【发布时间】:2018-02-14 13:03:20
【问题描述】:

我的 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;

}

我不明白为什么它不停。

任何帮助表示赞赏。

【问题讨论】:

  • 1.) 确保您没有对可能创建重复侦听器的startLocationUpdates() 进行冗余调用 2.) removeLocationUpdates 返回一个任务,它允许您检查调用是否成功 3. ) 我不认为在LocationCallback 中调用stopLocationUpdates() 是个好主意。

标签: android android-service fusedlocationproviderclient


【解决方案1】:

这是我所做的:

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(),它会自己照顾自己,然后摧毁一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多