我正在尝试访问Manifest.permission中的权限列表,这是一个最终类。如何访问其声明的String值?
答案 0 :(得分:1)
从字面上看,我认为问题是如何简单地获取POSSIBLE权限列表,而不是包的运行时权限。假设这是真的,这只是一个反思问题。
你会做这样的事情(未编译):
List<Field> permissionFields = new ArrayList<Field>();
Field[] fields = Manifest.permission.class.getDeclaredFields();
for (Field declField : fields) {
if ((declField.getType() == String.class) && Modifier.isFinal(declField.getModifiers())) {
permissionFields.add(declField);
}
}
“permissionFields”列表包含作为权限值的对象列表。您可以使用“Field.getObject(obj)”方法来获取String值(奇怪的是,似乎没有“Field.getString(obj)”方法,尽管还有其他几个“Field.get(obj) )“方法(可能因为String不是原始的))。
答案 1 :(得分:0)
以下是代码:
Log.d(TAG, "Getting permissions for " + packageName);
try {
PackageInfo pkgInfo = getPackageManager().getPackageInfo(
packageName,
PackageManager.GET_PERMISSIONS
);
PermissionInfo[] permissions = pkgInfo.permissions;
if (permissions == null) {
permissionsField.setText("No declared permissions");
}
else {
String permissionsText = "";
for (int i = 0; i < permissions.length; i++) {
switch (permissions[i].protectionLevel) {
case 0:
permissionsText += permissions[i].name + ":::" + "NORMAL" + "\n";
break;
case 1:
permissionsText += permissions[i].name + ":::" + "DANGEROUS" + "\n";
break;
case 2:
permissionsText += permissions[i].name + ":::" + "SIGNATURE" + "\n";
break;
case 3:
permissionsText += permissions[i].name + ":::" + "SIGNATUREORSYSTEM" + "\n";
break;
}
}
permissionsField.setText(permissionsText);
}
String[] requestedPermissions = pkgInfo.requestedPermissions;
if (requestedPermissions == null) {
requestedPermissionsField.setText("No requested permissions");
}
else {
String reqPermText = "";
for (int i = 0; i < requestedPermissions.length; i++) {
reqPermText += requestedPermissions[i] + "\n";
}
requestedPermissionsField.setText(reqPermText);
}
}
catch (PackageManager.NameNotFoundException e) {
permissionsField.setText("Package Not Found");
requestedPermissionsField.setText("Package Not Found");
}