在不询问用户的情况下打开设备的位置访问选项



我正在为Android Kitkat版本(最小API级别19)开发一个Android应用程序,其中我需要在不询问用户的情况下自动打开用户设备的位置访问选项。我尝试了此代码(下面给出),但它弹出了一个对话框并询问用户。因此,请告诉我如何在不询问用户的情况下打开设备的位置访问。

代码:

public static void enableDeviceLocation(final Activity activity) {
    final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity.getApplicationContext()).addApi(LocationServices.API).build();
    googleApiClient.connect();
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    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 result) {
            Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    //All location settings are satisfied.
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    //Location settings are not satisfied. Show the user a dialog to upgrade location settings
                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
                        status.startResolutionForResult(activity, LocationRequest.PRIORITY_HIGH_ACCURACY);
                    }
                    catch (IntentSender.SendIntentException e) {
                        //PendingIntent unable to execute request.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    //Location settings are inadequate, and cannot be fixed here. Dialog not created.
                    break;
            }
            googleApiClient.disconnect();
        }
    });
}

在标准未根的Android设备上,出于明显的隐私和安全原因,您无法自动启用任何位置提供商。

最新更新