【问题标题】:How can I listen assigned location permisson for the HMS Core?如何监听 HMS Core 分配的位置权限?
【发布时间】:2021-10-19 14:38:31
【问题描述】:

我尝试将 GMS 应用程序迁移到 HMS 应用程序,包括地图和定位服务,但据我了解,要通过华为定位工具包获取用户位置,用户需要为 HMS Core 应用程序分配位置权限,但我无法遵循此权限是在我的应用程序上分配与否,当地图准备好时,我会检查位置,但会提示并警告将位置权限分配给 HMS Core,我无法在分配的此权限中收听并且它崩溃了,所以我想问一下如何我听用户将位置权限分配给 HMS Core 应用程序?或者我可以用另一种方式解决这个问题吗?我可以在没有 HMS Core 应用位置权限的情况下获取用户 GPS 位置吗?或者有任何关于监听用户为HMS核心应用分配位置权限的回调

【问题讨论】:

    标签: firebase huawei-mobile-services huawei-developers huawei-map-kit huawei-location-kit


    【解决方案1】:

    权限请求和管理与 GMS 应用完全相同,由 Android 操作系统处理。如果您的原始 GMS 应用程序具有检查位置权限的代码,那应该没问题。以下是如何设置 GMS 和 HMS 位置权限检查的两个示例。您可以看到它们彼此非常相似。

    堆栈溢出 - GMS 位置权限示例https://stackoverflow.com/a/33070595/14880637

    华为开发者-HMS Location Kit设置指南https://developer.huawei.com/

    【讨论】:

    • 谢谢,是的,GMS 和 HMS 定位套件类似,但是 HMS Core(华为移动服务)没有默认分配定位权限,所以 HMS 定位有两个问题首先我需要检查 GPS权限,其次我需要检查 HMS Core 位置权限,HMS 需要额外的努力,因为 HMS 没有自己分配的默认位置权限
    【解决方案2】:

    checkLocationSettings 方法可以帮到你。

    1. 调用getSettingsClient()方法获取SettingsClient实例。
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    
    1. 致电checkLocationSettings()检查设备位置设置。
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    mLocationRequest = new LocationRequest();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();
    // Check the device location settings.
    settingsClient.checkLocationSettings(locationSettingsRequest)
        // Define the listener for success in calling the API for checking device location settings.
        .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                    LocationSettingsStates locationSettingsStates =
                            locationSettingsResponse.getLocationSettingsStates();
                    StringBuilder stringBuilder = new StringBuilder();
                    // Checks whether the location function is enabled. 
                    stringBuilder.append(",\nisLocationUsable=")
                            .append(locationSettingsStates.isLocationUsable());
                    // Checks whether HMS Core (APK) is available. 
                    stringBuilder.append(",\nisHMSLocationUsable=")
                            .append(locationSettingsStates.isHMSLocationUsable());
                    Log.i(TAG, "checkLocationSetting onComplete:" + stringBuilder.toString());
            }
        })
        // Define callback for failure in checking the device location settings.
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                // Processing when the device is a Huawei device and has HMS Core (APK) installed, but its settings do not meet the location requirements.
                int statusCode = ((ApiException) e).getStatusCode();
                switch (statusCode) {
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        try {
                            ResolvableApiException rae = (ResolvableApiException) e;
                            // Call startResolutionForResult to display a popup asking the user to enable related permission.
                            rae.startResolutionForResult(MainActivity.this, 0);
                        } catch (IntentSender.SendIntentException sie) {
                            // TODO
                        }
                    break;
                }
            }
        });
    

    可以查看this Docs了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-21
      • 2019-08-21
      • 2017-04-04
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      相关资源
      最近更新 更多