华为设备应用位置权限错误如何解决?

How to resolve the location permission error for Apps on Huawei device?

我的 huawei phone 上的 Huawei HMS core 没有默认权限,我正在使用 HMS 定位工具包编写我的应用程序,并且总是出现定位工具包的权限错误。我按照他们的开发指南在清单文件中设置了位置权限。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

并遵循他们的代码示例:

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    Log.i(TAG, "sdk < 28 Q");
    if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        || ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        String[] strings =
            {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        ActivityCompat.requestPermissions(this, strings, 1);
    }
} else {
    if (ActivityCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
        || ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
        || ActivityCompat.checkSelfPermission(this,
            "android.permission.ACCESS_BACKGROUND_LOCATION") != PackageManager.PERMISSION_GRANTED) {
        String[] strings = {android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            "android.permission.ACCESS_BACKGROUND_LOCATION"};
        ActivityCompat.requestPermissions(this, strings, 2);
    }
}

有什么建议吗?

  1. 必须始终允许 HMS 权限。否则会报错

    确保HMS Core(APK)已分配定位权限。为此,请转到 设置 > 应用程序 > 应用程序 并找到 HMS Core 。 (菜单路径可能因操作系统版本不同而有所不同,如未找到HMS Core,点击Apps[=45=右上角的菜单图标] 并点击 显示系统进程。)然后,点击 HMS Core (APK) 图标,转到 应用程序信息 > 权限 > Location,验证位置权限是否分配给HMS Core。在 运行 EMUI 10.0 或更高版本的设备上,Location 必须设置为 Always for HMS Core[=45] =].

  1. 确保下拉通知栏中的位置信息开关已打开。

  2. 模拟位置需要模拟权限。否则报error权限错误

  1. 检查您的应用是否启用了位置权限。

Shirley 的回答涵盖了 HMS 位置权限的设备端。为了覆盖所有低版本EMUI用户HMS Core默认没有定位权限的这种场景,可以使用API“settingsClient.checkLocationSettings(…)”获取设备定位权限。之后,即使HMS Core App的位置权限被禁用,您的应用也可以提示用户一键开启相关权限。

请参考HMS Location guide

第一步:获取SettingsClient服务API。

SettingsClient settingsClient = LocationServices.getSettingsClient(this);

第 2 步:调用 checkLocationSettings() 检查设备设置。

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();

mLocationRequest = new LocationRequest();

builder.addLocationRequest(mLocationRequest);

LocationSettingsRequest locationSettingsRequest = builder.build();

//check Location Settings

settingsClient.checkLocationSettings(locationSettingsRequest)

        .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {

            @Override

            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {

                //Have permissions, send requests

                fusedLocationProviderClient

                        .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())

                        .addOnSuccessListener(new OnSuccessListener<Void>() {

                            @Override

                            public void onSuccess(Void aVoid) {

                                //Interface call successfully processed

                            }

                        });

            }

        })

        .addOnFailureListener(new OnFailureListener() {

            @Override

            public void onFailure(Exception e) {

                //Settings do not meet targeting criteria

                int statusCode = ((ApiException) e).getStatusCode();

                switch (statusCode) {

                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:

                        try {

                            ResolvableApiException rae = (ResolvableApiException) e;

                            //Calling startResolutionForResult can pop up a window to prompt the user to open the corresponding permissions

                            rae.startResolutionForResult(MainActivity.this, 0);

                        } catch (IntentSender.SendIntentException sie) {

                            //…

                        }

                        break;

                }

            }

        });