串联使用 Google 设置 API 和 PermissionModel API

Using Google SettingsAPI and PermissionModel API in tandem

我正在查看可在此处找到的 Play Services 7.0 文档:

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

正如我们所见,Android 简化了应用程序启用位置的方式,只需轻按一下即可,这很棒。

然而我无法理解的是这将如何与权限模型一起工作。

我会尽量使我的查询简单明了。

例如引用文档:

要使用此 API,首先创建一个至少支持 LocationServices.APIGoogleApiClient。然后将客户端连接到 Google Play services:

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);

然后检查是否满足当前位置设置:

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

PendingResult returns 时,客户端可以通过查看来自 LocationSettingsResult 对象的状态代码来检查位置设置。客户端还可以通过调用 getLocationSettingsStates():

来检索相关位置设置的当前状态
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
     @Override
     public void onResult(LocationSettingsResult result) {
         final Status status = result.getStatus();
         final LocationSettingsStates = 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(
                         OuterClass.this,
                         REQUEST_CHECK_SETTINGS);
                 } catch (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;
         }
     }
 });

如果状态码是RESOLUTION_REQUIRED,客户端可以调用startResolutionForResult(Activity, int)来弹出一个对话框,请求用户允许修改位置设置以满足这些请求。对话的结果将通过 onActivityResult(int, int, Intent) 返回。如果客户端对可用的位置提供者感兴趣,它可以通过调用 fromIntent(Intent):

从 Intent 中检索 LocationSettingsStates
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
     switch (requestCode) {
         case REQUEST_CHECK_SETTINGS:
             switch (resultCode) {
                 case Activity.RESULT_OK:
                     // All required changes were successfully made
                     ...
                     break;
                 case Activity.RESULT_CANCELED:
                     // The user was asked to change settings, but chose not to
                     ...
                     break;
                 default:
                     break;
             }
             break;
     }
 }

上面的代码片段将向用户显示一个带有“是”和“否”选项的位置对话框,单击“是”即可启用位置。但是文档没有说明任何关于实际创建该对话框的内容。是系统自己处理的吗?如果可以,需要用什么方法?

此外,当尝试将 LocationServicesAPI 与权限模型一起使用时,流程应该如何工作?

这是否意味着我们首先显示请求 ACCESS_FINE_LOCATION permission 的权限模型对话框,如果该权限被授予,我们然后再显示 "location needs to be enabled" 对话框?这是 Android 推荐的方式吗?我没有看到任何相关文档。

TL;DR

  1. 我们如何使用新的权限模型API和LocationServicesAPI一起使用,因为它们都涉及多个popus。有android推荐的方法吗?

  2. 如果用户授予 ACCESS_FINE_LOCATION 权限但不同意启用位置,该流程应该如何工作。

  3. 如果用户授予 ACCESS_FINE_LOCATION 权限并同意最初启用位置,但后来禁用位置,该流程应该如何工作。

  4. 我们如何创建一键式事件对话框来要求用户启用位置服务(我不是指权限对话框)。 Google 地图应用程序使用的对话框似乎是系统提供的对话框。但是我找不到任何文档。

任何答案,对此的想法将不胜感激!

如果有任何需要更正的地方,请发表评论而不是投反对票。

谢谢!

  1. How do we use the new permission model API and the LocationServicesAPI together, since both of them involve multiple popus. Is there an android recommended way?

他们是完全独立的。 SettingsApi 根本不需要任何运行时权限,因为您实际上并未获取任何位置数据。它只允许您要求用户启用适当的 services/settings 以便您的 LocationRequest 将接收到它期望的数据质量。

我认为您要问的更大的问题是:"if I should ask for both, which order should I ask them in?"我认为,在大多数情况下,首先请求运行时权限然后请求启用位置设置是最有意义的,因为运行时权限会需要 才能使用对用户位置设置的任何更改。首先请求位置权限还可以为用户提供他们需要了解的上下文为什么您请求他们启用位置设置。

  1. If the user grants the ACCESS_FINE_LOCATION permission but does not agree to enable location, how is that flow supposed to work.

那么您将不会获得任何数据。

  1. If the user grants the ACCESS_FINE_LOCATION permission and agrees to enable location initially, but disables location later on, how is that flow supposed to work.

如果要确保获取位置数据,则每次都应检查运行时权限并使用 SettingsApi。

  1. How do we create the one-tap affair dialog for asking user to enable location services (I am NOT REFERRING to the permission dialog). The Google Maps app uses a dialog which seems to be a system provided dialog. I however cannot find any documentation to it.

SettingsApi 是开发人员在系统级别启用位置设置的最接近选项。