0

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

4

2 回答 2

0

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

可以查看此文档以获取更多信息。

于 2021-10-20T06:32:53.463 回答
0

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

Stack Overflow - GMS 位置权限示例https://stackoverflow.com/a/33070595/14880637

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

于 2021-10-19T21:02:27.740 回答