它可以使用PackageManager getAllPermissionGroups( )和queryPermissionsByGroup() 來枚舉整個Android權限層次結構。下面的代碼在5.1.1(SDK 22)設備的末尾生成了輸出結果。標記爲「個人」的組具有groupInfo.flags == 1,並且看起來與在棉花糖中被稱爲「危險」的權限組相對應。
由於SDK級別的差異以及應用程序可以定義自定義權限的事實,您將在不同設備上獲得不同的權限層次結構。
/**
* Uses PackageManager getAllPermissionGroups() and queryPermissionsByGroup()
* to enumerate the Android permission hierarchy.
*/
private void showPermissionTree()
{
final PackageManager pm = m_context.getPackageManager();
if (pm == null)
return;
/*
* Get a list of all permission groups and sort them alphabetically.
* Then add to the end of the list the special case of a null group name. There can be
* numerous permissions that are not listed under a group name.
*/
List<PermissionGroupInfo> groupInfoList = pm.getAllPermissionGroups(0);
if (groupInfoList == null)
return;
ArrayList<String> groupNameList = new ArrayList<>();
for (PermissionGroupInfo groupInfo : groupInfoList) {
String groupName = groupInfo.name;
if (groupName != null) {
if (Build.VERSION.SDK_INT >= 17) {
/*
* SDK 17 added the flags field. If non-zero, the permission group contains
* permissions that control access to user personal data.
* N.B. These are the permissions groups that are called "dangerous" in
* Marshmallow.
*/
if (groupInfo.flags != 0) {
groupName += " (personal)";
}
}
groupNameList.add(groupName);
}
}
Collections.sort(groupNameList);
groupNameList.add(null);
/*
* Loop though each permission group, adding to the StringBuilder the group name and
* the list of all permissions under that group.
*/
StringBuilder sb = new StringBuilder(10000);
final String INDENT = " ";
for (String groupName : groupNameList) {
if (groupName == null)
groupName = "null";
sb.append("* ").append(groupName).append("\n");
ArrayList<String> permissionNameList = getPermissionsForGroup(groupName);
if (permissionNameList.size() > 0) {
for (String permission : permissionNameList) {
sb.append(INDENT).append(permission).append("\n");
}
} else {
sb.append(INDENT).append("no permissions under group\n");
}
sb.append("\n");
}
m_textView.setText(sb.toString());
}
/*
* Gets and returns a list of all permission under the specified group, sorted alphabetically.
*
* N.B. groupName can be null. The docs for PackageManager.queryPermissionsByGroup() say
* "Use null to find all of the permissions not associated with a group."
*/
private ArrayList<String> getPermissionsForGroup(String groupName)
{
final PackageManager pm = m_context.getPackageManager();
final ArrayList<String> permissionNameList = new ArrayList<>();
try {
List<PermissionInfo> permissionInfoList =
pm.queryPermissionsByGroup(groupName, PackageManager.GET_META_DATA);
if (permissionInfoList != null) {
for (PermissionInfo permInfo : permissionInfoList) {
String permName = permInfo.name;
if (permName == null) {
permName = "null";
} else if (permName.isEmpty()) {
permName = "empty";
}
permissionNameList.add(permName);
}
}
}
catch (PackageManager.NameNotFoundException e) {
// e.printStackTrace();
Log.d(TAG, "permissions not found for group = " + groupName);
}
Collections.sort(permissionNameList);
return permissionNameList;
}
3210
這是對個人權限組合成一組的方式。我想知道,開發人員論壇中提到的組和個人權限之間已經存在映射。例如,有一個名爲「BLUETOOTH_NETWORK」的許可組。該組最有可能包含三個權限,即「BLUETOOTH」,「BLUETOOTH_ADMIN」和「BLUETOOTH_PRIVILEGED」。我自己繪製了這些圖。如果有任何資源可用,我想完成映射。 – Faheem
@Faheem我不知道用法,但任何人都可以自由地將權限映射到權限組。使用頁面中給出的描述來搜索關鍵字。例如;搜索「短信」,你會發現相關的權限。 – Shervin