应用的权限种类
-
Normal permissions
只需要在AndroidManifest.xml 中声明,如INTERNET,WAKE_LOCK等
-
Signature permissions
申请该权限的应用和定义该权限的应用有相同签名时,这个权限才会被授予,一些Signature permissions不能被三方应用使用
-
Dangerous permissions
需要申请运行时权限
-
Special permissions 如SYSTEM_ALERT_WINDOW需要应用发送Settings.ACTION_MANAGE_OVERLAY_PERMISSION intent去提示用户是否开启该权限
权限列表可以参考官网:
https://developer.android.google.cn/guide/topics/permissions/overview?hl=en
设置中应用权限管理
主要分析的类
Permission.java -权限的实体类,权限是否已申请,权限的过滤
AppPermissions.java -
AppPermissionGroup.java -权限的管理
先来分析AppPermissionGroup,创建 传入cont ext,PackageInfo ,permissionName,获取过滤权限信息
public static AppPermissionGroup create(Context context, PackageInfo packageInfo,
String permissionName) {
PermissionInfo permissionInfo;
try {
permissionInfo = context.getPackageManager().getPermissionInfo(permissionName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
if (permissionInfo.protectionLevel != PermissionInfo.PROTECTION_DANGEROUS
|| (permissionInfo.flags & PermissionInfo.FLAG_INSTALLED) == 0
|| (permissionInfo.flags & PermissionInfo.FLAG_HIDDEN) != 0) {
return null;
}
PackageItemInfo groupInfo = permissionInfo;
if (permissionInfo.group != null) {
try {
groupInfo = context.getPackageManager().getPermissionGroupInfo(
permissionInfo.group, 0);
} catch (PackageManager.NameNotFoundException e) {
/* ignore */
}
}
List<PermissionInfo> permissionInfos = null;
if (groupInfo instanceof PermissionGroupInfo) {
try {
permissionInfos = context.getPackageManager().queryPermissionsByGroup(
groupInfo.name, 0);
} catch (PackageManager.NameNotFoundException e) {
/* ignore */
}
}
return create(context, packageInfo, groupInfo, permissionInfos,
new UserHandle(context.getUserId()));
}
权限的申请和关闭
final AppPermissionGroup group = mAppPermissions.getPermissionGroup(groupName);
if (group == null) {
return;
}
if (check) {
group.revokeRuntimePermissions(false);//关闭
} else {
group.grantRuntimePermissions(false);//打开
}