我需要从权限名称中找到联系人,存储等权限组名称。我有以下代码
public ArrayList<String> permissionGroup(ArrayList<String> permission) {
ArrayList<String>groupPermission = new ArrayList<>();
PackageManager pm = getPackageManager();
for(String entry : permission){
PermissionGroupInfo info = null;
try {
info =pm.getPermissionGroupInfo(entry,0);
} catch (PackageManager.NameNotFoundException e) {
Log.w("infoerror ", e.getStackTrace().toString());
}
if(info == null){
Log.w("infonull","error");
}
else {
String loadLabelVal = info.loadLabel(pm).toString();
Log.w("groupName ", loadLabelVal);
if (!groupPermission.contains(loadLabelVal)) {
groupPermission.add(loadLabelVal);
}
}
}
return groupPermission;
}
这里te函数的输入是需要找到相应组的权限的arraylist。我将pm.getPermissionGroupInfo(entry,0)视为null。 我不知道这是什么错误。请纠正我。
提前致谢。
答案 0 :(得分:4)
要获取“短名称”,例如“位置”,您可以使用
PackageManager packageManager = getApplicationContext().getPackageManager();
PermissionInfo permissionInfo = packageManager.getPermissionInfo(Manifest.permission.ACCESS_FINE_LOCATION, 0);
PermissionGroupInfo permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0);
Log.w("11_APP", ""+permissionGroupInfo.loadLabel(packageManager));