更改位置设置 - Android教程

时间:2016-04-25 04:19:53

标签: android location

我目前正在尝试实现请求更改位置设置的代码。我目前正在关注Android Studio Tutoriel。

然而,当我按照教程并将代码放在实际的Studio中时,我发现它出现了一些错误。

protected void createLocationRequest() {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder LSBuilder = new LocationSettingsRequest.Builder();
        LSBuilder.addLocationRequest(mLocationRequest);
        final PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,LSBuilder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                final LocationSettingsStates = result.getLocationSettingsStates();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        // All location settings are satisfied. The client can
                        // initialize location requests here.
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        // Location settings are not satisfied, but this can be fixed
                        // by showing the user a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),
                            // and check the result in onActivityResult().
                            status.startResolutionForResult(
                                    MainActivity.this,
                                    REQUEST_CHECK_SETTINGS);
                        } catch (SendIntentException e) {
                            // Ignore the error.
                        }
                        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.

                        break;
                }
            }
        });

首先,

final LocationSettingsStates = result.getLocationSettingsStates();

出现意外令牌错误。我不太清楚这里有什么问题。似乎有任何其他信息。其次,

status.startResolutionForResult(MainActivity.this,REQUEST_CHECK_SETTINGS);

在那里,REQUEST_CHECK_SETTINGS未定义。我不太清楚我应该把它定义为什么。

任何人都可以解释上述两个错误吗?

1 个答案:

答案 0 :(得分:1)

LocationSettingsStates只是一种类型,您需要指定存储该类型对象的位置。替换:

final LocationSettingsStates = result.getLocationSettingsStates();

使用:

final LocationSettingsStates states = result.getLocationSettingsStates();

我相信REQUEST_CHECK_SETTINGS只是您为回调设置的唯一标识符,因此请在您的课程中将其声明为:

private final int REQUEST_CHECK_SETTINGS = 0; // a unique identifier