Android LocationManager locationManager.requestLocationUpdates() 不工作

Android LocationManager locationManager.requestLocationUpdates() not working

大家好,我正在尝试通过位置管理器获取当前位置,我正在使用下面的代码

LocationManager locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider =locationManager.getBestProvider(criteria,true);

        Location location = 
locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

但是我没有收到这条线的任何回复

locationManager.requestLocationUpdates(bestProvider, 20000, 0,this);

并且还将最后已知位置设为空,我已经在清单和动态中添加了所有权限,但是上面这行没有给出任何响应,我搜索了它但是没有得到相关答案,请帮忙。

对于getLastKnownLocation方法,在documentation中说:

If the provider is currently disabled, null is returned.

这意味着您的 GPS 已禁用。

对于 requestLocationUpdates,您每 20 秒请求一次位置,请尝试减少此数字,以便您至少知道您的程序是否正常工作。

你可以试试这个:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

此外,我真的建议您使用新的 API 来请求位置更新:https://developer.android.com/training/location/receive-location-updates.html

它非常快速和准确,但您需要创建一个 google 开发者帐户(免费)并创建一个 api 密钥。所有需要的信息都应该在 link.

String bestProvider =locationManager.getBestProvider(criteria,true);

在上一行中,第二个布尔参数为真。这意味着它将获取已启用的提供程序。而你 GPS_PROVIDER 被禁用了 locationManager.getLastKnownLocation returns null.

此外,由于您请求 BestProvider 的条件为空,因此您必须将 "passive" 作为 BestProvider。这意味着只有当其他应用程序请求和接收更新时,您才会收到位置更新。

在 class 的 onLocationChangedMethod() 中获取真实位置更新。您必须确保这些事情:

  1. 确保您有 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 之一,然后在运行时也请求它,您说您已经完成了。
  2. 如果您想通过 GPS_PROVIDER 请求位置,那么您需要确保 GPS_PROVIDER 已启用,您可以使用以下代码执行此操作:

    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // OR some other PRIORITY depending upon your requirement
    
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);
    
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(googleApiClient,
                    builder.build());
    
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult locationSettingsResult) {
            final Status status = locationSettingsResult.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can
                    // Request for location updates now
    
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    status.startResolutionForResult(YourActivity.this, 100); // First parameter is your activity instance
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way
                    // to fix the settings so we won't show the dialog.
                    Log.e("TAG", "Error: Can't enable location updates SETTINGS_CHANGE_UNAVAILABLE");
                    break;
            }
        }
    });
    

LocationManager.NETWORK_PROVIDER需要网络,但是是真实的,不精确。 如果你想使用LocationManager.GPS_PROVIDER,情况必须是室外而不是室内,因为GPS定位需要卫星,如果你在任何建筑物中,卫星都找不到你! 请到户外再与GPS_PROVIDER确认!

*最好的方法是从 GPS 和网络获取位置,然后检查其中任何一个不为空并且使用它更准确。