您可以使用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;