Android LocationManager locationManager.requestLocationUpdates()不起作用

时间:2017-05-10 05:57:47

标签: android locationmanager

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

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

并且将上次知道的位置设为null,我已经在清单中添加了所有权限以及动态,但是上面这行没有给出任何响应,我搜索了它但得到了相关答案,请帮助。

2 个答案:

答案 0 :(得分:1)

对于getLastKnownLocation方法,在documentation中说:

  

如果当前禁用了提供程序,则返回null。

这意味着您的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

这是非常快速和准确的,但您需要创建一个谷歌开发人员帐户(免费)并创建一个API密钥。所有需要的信息都应该在链接中。

答案 1 :(得分:1)

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

在上面的行中,第二个布尔参数为true。这意味着它将获取已启用的提供程序。并且您禁用了GPS_PROVIDER,这可以通过locationManager.getLastKnownLocation返回null来验证。

此外,由于您的请求BestProvier的条件为空,因此您必须作为BestProvider获得“被动”。这意味着只有在其他应用程序请求并接收更新时,您才会收到位置更新。

在您班级的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;
            }
        }
    });