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

时间:2012-11-23 06:03:21

标签: android permissions

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

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

            }

        }

任何人都可以帮助我...只是想在许可面前添加保护级别。

2 个答案:

答案 0 :(得分:4)

您可以使用PackageManagergetPermissionInfo()方法获取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);
}

以下行仅适用于16级或更高级别的API:

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

答案 1 :(得分:-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);
  }
}
相关问题