6

我正在使用 FusedLocationAPI 它工作正常,但是当用户禁用位置时,我正在使用 SettingsApi 向用户显示对话框以启用位置。

在棉花糖及以上设备中它工作正常,但在 kitkat 中,如果启用或禁用位置,它总是触发 LocationSettingsStatusCodes.RESOLUTION_REQUIRED。

  LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult locationSettingsResult) {

            final Status status = locationSettingsResult.getStatus();
            Log.e("status",status.getStatusCode()+"\n"+status.getStatus()+"\n"+status.getStatusMessage());
            final LocationSettingsStates LS_state = locationSettingsResult.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    startLocationUpdates();

                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could 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 (IntentSender.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;
            }
        }
    });



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PERMISSION_SETTING) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, permissionsRequired[0]) == PackageManager.PERMISSION_GRANTED) {
            //Got Permission
            proceedAfterPermission();
        }
    } else if (requestCode == REQUEST_CHECK_SETTINGS) {
        switch (resultCode) {
            case RESULT_OK:
                // All required changes were successfully made
                startLocationUpdates();
                break;
            case RESULT_CANCELED:
                // The user was asked to change settings, but chose not to
                Toast.makeText(this, "User cancelled location enabled", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}

在 kitkat 中它显示这样的对话框图片但是当点击确定时它触发用户取消位置

4

0 回答 0