如何提供启用 GPS 的选项

How to give option for enable GPS

我想在 GPS 禁用时显示打开 GPS 的对话框。

见图片。当我单击“是”时,应该会自动打开我的 GPS,而无需进入设置。

那么,如何通过 select YES 选项启用 GPS?

这是 Google 播放服务 api 的一部分,因为 7.0 称为 SettingsApi

位置设置 - 虽然 FusedLocationProviderApi 结合多个传感器为您提供最佳位置,但您的应用接收到的位置的准确性仍然在很大程度上取决于设备上启用的设置(GPS、wifi、飞行模式, 和别的)。使用新的 SettingsApi class,您可以调出一个位置设置对话框,该对话框显示一键式控件,用户无需离开您的应用程序即可更改他们的设置。

要使用此 API,首先创建一个至少支持 LocationServices.API 的 GoogleApiClient。然后将客户端连接到 Google 播放服务:

mGoogleApiClient = new GoogleApiClient.Builder(context)
     .addApi(LocationServices.API)
     .addConnectionCallbacks(this)
     .addOnConnectionFailedListener(this)
     .build()
 ...
 mGoogleApiClient.connect();

然后创建一个 LocationSettingsRequest.Builder 并添加应用程序将使用的所有 LocationRequests:

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
     .addLocationRequest(mLocationRequestHighAccuracy)
     .addLocationRequest(mLocationRequestBalancedPowerAccuracy);

后续步骤请查看此处:https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

重要提示:如果状态码是RESOLUTION_REQUIRED,客户端可以调用startResolutionForResult(Activity, int)来bring up a dialog, asking for user's permission to modify the location settings to satisfy those requests.对话的结果会通过on[=返回36=]结果(整数、整数、意图)。如果客户端对可用的位置提供者感兴趣,它可以通过调用 fromIntent(Intent)

从 Intent 中检索 LocationSettingsStates

另请参考官方回购:https://github.com/googlemaps/android-samples/tree/master/ApiDemos 如果您想在 运行 时间授予 API 23(Android M) 的权限。