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

            }

        }

任何人都可以帮我解决这个问题...只想在权限前添加保护级别。

4

2 回答 2

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;
于 2012-11-23T06:23:08.503 回答
-1

// 获取核心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);
  }
}
于 2012-11-23T06:24:09.500 回答