我试图列出所选应用程序中每个权限的保护级别,对于下面给出的代码。但我不知道如何完成它。
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);
}
}
}
谁能帮我解决这个问题...只想在权限前面添加保护级别。
您可以使用包管理器 http://developer.android.com/reference/android/content/pm/PackageManager.html class 获取权限信息() http://developer.android.com/reference/android/content/pm/PackageManager.html#getPermissionInfo%28java.lang.String,%20int%29方法得到权限信息 http://developer.android.com/reference/android/content/pm/PermissionInfo.html任何特定权限的对象。权限信息 http://developer.android.com/reference/android/content/pm/PermissionInfo.html对象有属性防护等级 http://developer.android.com/reference/android/content/pm/PermissionInfo.html#protectionLevel可用于检查任何权限的保护级别...您可以根据中定义的常量进行检查许可信息 http://developer.android.com/reference/android/content/pm/PermissionInfo.html类如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);
}
UPDATE:
要获得以下保护级别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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)