如何获得每个权限的保护级别? [英] How to get the protection level for each permission?

查看:132
本文介绍了如何获得每个权限的保护级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对以下给出的代码列出所选应用程序中每个权限的保护级别.但是我不知道该怎么做.

Im trying to list the protection level for each permission in the selected application, for the code given below. But i do not know how to get it done.

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

            }

        }

有人可以帮我吗……只是想在权限之前添加保护级别.

can anyone help me with this... just want to add the protection level in front of the permission.

推荐答案

您可以使用 getPermissionInfo()方法来获取 PermissionInfo 对象以获取任何特定权限. PermissionInfo 对象具有属性 PermissoinInfo 类中定义的常量进行检查PROTECTION_FLAG_SYSTEM.

You can use PackageManager class getPermissionInfo() method to get PermissionInfo object for any particular permission. PermissionInfo object has property Protection Lavel that can be used to check the protection level of any permission... You can check it against the constant defined in the PermissoinInfo class like 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或更高版本:

Following line would only work on API level 16 or above:

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

这篇关于如何获得每个权限的保护级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆