Android:以编程方式启用位置设置,无需离开应用程序或转到设置屏幕

Android :Enable location settings programatically with out leaving app or going to settings screen

也许已经回答了,但是是否有官方 api 可以在不进入设置屏幕的情况下以编程方式启用位置设置。

什么是适用于 2.3 及更高版本 android 版本

的最佳方法

谢谢

尼特兹

在 google play services 7.0 中发现这个...fusedapiprovider/settingsapi api 可以使用.. http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html?m=1 https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

编码愉快..

这会有所帮助

public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(30 * 1000);
            locationRequest.setFastestInterval(5 * 1000);
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            builder.setAlwaysShow(true);
            PendingResult<LocationSettingsResult> result =
                    LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
            result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
                @Override
                public void onResult(LocationSettingsResult result) {
                    final Status status = result.getStatus();
                    final LocationSettingsStates state = 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 could be fixed by showing the user
                            // a dialog.
                            try {
                                // Show the dialog by calling startResolutionForResult(),
                                // and check the result in onActivityResult().
                                status.startResolutionForResult(
                                        activity, 1000);
                            } 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;
                    }
                }
            }

);
    }
googleApiClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

locationChecker().

结束前添加以上代码行