这是自 7.0 以来 Google Play 服务 API 的一部分,称为 SettingsApi。
位置设置 - 虽然FusedLocationProviderApi 结合了多个传感器为您提供最佳位置,但您的应用接收到的位置的准确性仍然很大程度上取决于设备上启用的设置(GPS、wifi、飞行模式等) .使用新的 SettingsApi 类,您可以调出位置设置对话框,该对话框显示一键式控件,用户无需离开您的应用即可更改其设置。
要使用此 API,首先创建一个至少支持 LocationServices.API 的 GoogleApiClient。然后将客户端连接到 Google Play 服务:
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build()
...
mGoogleApiClient.connect();
然后创建一个LocationSettingsRequest.Builder 并添加应用将使用的所有 LocationRequest:
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.对话结果会通过onActivityResult(int, int, Intent)返回。如果客户端对哪些位置提供程序可用感兴趣,它可以通过调用 fromIntent(Intent) 从 Intent 中检索 LocationSettingsStates
另外请参考这个官方repo:https://github.com/googlemaps/android-samples/tree/master/ApiDemos
如果您想在运行时授予 API 23(Android M) 的权限。