如何获得每个许可的保护级别?(How to get the protection level for

2019-08-03 03:48发布

我试着列出所选应用程序的每个权限的保护级别,下面给出的代码。 但我不知道如何把它做。

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

            }

        }

谁能帮我这个...只是要添加的权限前面的保护级别。

Answer 1:

您可以使用PackageManager类getPermissionInfo()方法来获得PermissionInfo对象为任何特定的权限。 PermissionInfo对象拥有产权保护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;


Answer 2:

//获取核心Android包的权限

PackageInfo packageInfo = getPackageManager().getPackageInfo("android", PackageManager.GET_PERMISSIONS);
if (packageInfo.permissions != null) {
  // For each defined permission
  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);
  }
}


文章来源: How to get the protection level for each permission?