如何获取每个权限的保护级别?

5

我正在尝试列出所选应用程序中每个权限的保护级别,对于下面给出的代码。但我不知道如何完成它。

ArrayList<String> list_permission = new ArrayList<String>();
        String[] reqp = info.requestedPermissions;
        if (reqp != null) {
            for (i = 0; i < reqp.length; i++) {

                k = i + 1;

                String a = reqp[i];
                if (a.contains("android.permission.")) {
                    String aa[] = a.split("android.permission.");
                    list_permission.add(aa[1]);
                } else {
                    list_permission.add(a);
                }

            }

        }

有人能帮我吗...我只想在权限前面添加保护级别。


你想检查已安装软件包的权限级别吗?如果是,请查看此API http://developer.android.com/reference/android/content/pm/PermissionInfo.html#protectionLevel - Ketan Parmar
这篇文章提供了完整的Android权限保护级别,但我不知道如何在我正在开发的应用程序中编写它的代码。 - Romi
1个回答

5
你可以使用 PackageManager 类的 getPermissionInfo() 方法来获取任何特定权限的 PermissionInfo 对象。PermissionInfo 对象具有 Protection Lavel 属性,可用于检查任何权限的保护级别...您可以将其与 PermissoinInfo 类中定义的常量进行比较,例如 PROTECTION_FLAG_SYSTEM
例如以下代码:
for (PermissionInfo permission : packageInfo.permissions) {
    // Dump permission info
    String protectionLevel;
    switch(permission.protectionLevel) {
    case PermissionInfo.PROTECTION_NORMAL : protectionLevel = "normal"; break;
    case PermissionInfo.PROTECTION_DANGEROUS : protectionLevel = "dangerous"; break;
    case PermissionInfo.PROTECTION_SIGNATURE : protectionLevel = "signature"; break;
    case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protectionLevel = "signatureOrSystem"; break;
    default : protectionLevel = "<unknown>"; break;
    }
    Log.i("PermissionCheck", permission.name + " " + protectionLevel);
  }

更新:

获取requestedPermissions的保护级别:

String[] reqp = info.requestedPermissions;
String perm = reqp[i];
if (perm.contains("android.permission.")) {
    try {
        PermissionInfo pi = getPackageManager().getPermissionInfo(perm, PackageManager.GET_META_DATA);
        String protctionLevel = "unknown";

        switch(pi.protectionLevel) {
            case PermissionInfo.PROTECTION_NORMAL : protctionLevel = "normal"; break;
            case PermissionInfo.PROTECTION_DANGEROUS : protctionLevel = "dangerous"; break;
            case PermissionInfo.PROTECTION_SIGNATURE : protctionLevel = "signature"; break;
            case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM : protctionLevel = "signatureOrSystem"; break;
            case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break;
            default : protctionLevel = "<unknown>"; break;
        }
        list_permission.add(perm + "        "+protctionLevel);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

} else {
    list_permission.add(perm);
}

以下代码只能在API 16及以上版本中运行:

        case PermissionInfo.PROTECTION_FLAG_SYSTEM : protctionLevel = "system"; break;

所以它会给出整数值0、1、2、3,其中0表示正常,1表示危险,2表示签名,3表示系统? - Romi
请勿使用int值,而是将返回值与PermissionInfo类中定义的常量进行比较,以检查保护级别,类似于您分享的链接中所示... - Praful Bhatnagar
你分享的链接中的代码已经足够好了...如果你对那段代码有任何具体问题,请告诉我... - Praful Bhatnagar
嗯,我尝试过了,但不知道如何将权限保护级别设置为reqp[i]所选的权限。这就是我面临的问题。 - Romi
另外,如果我想将保护级别的颜色更改为红色和蓝色,该怎么做呢?比如“危险”(红色)。 - Romi
所有标记为“危险”的权限都被视为运行时权限吗?这意味着我们可以使用相同的API请求它们,并使用相同的API检查它们是否已授予。 - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接