Android 8.0系统 默认赋予应用权限

【Android 8.0系统 默认赋予应用权限】
private PackageInfo getCallingPackageInfo(String pkgName,Context context) {
try {
return context.getPackageManager().getPackageInfo(pkgName,
PackageManager.GET_PERMISSIONS);
} catch (NameNotFoundException e) {
Log.d("lei", "No package: " + e);
return null;
}
}
public void grantAllRuntimePermissions(String pkgName,Context context){
PackageManager pkgManager = context.getPackageManager();
PackageInfo mPackageInfo = getCallingPackageInfo(pkgName,context);
UserHandle userHandle = new UserHandle(
UserHandle.getUserId(mPackageInfo.applicationInfo.uid));
List requestPermissions = new ArrayList();
List permissionInfos = new ArrayList();
final int permissionCount = mPackageInfo.requestedPermissions.length;
for (int i = 0; i < permissionCount; i++) {
String requestedPermission = mPackageInfo.requestedPermissions[i];
try {
PermissionInfo permissionInfo = pkgManager.getPermissionInfo(requestedPermission, 0);
if(isRuntime(permissionInfo.protectionLevel) || isRuntimeOnly(permissionInfo.protectionLevel)){
Log.d("lei","runtime requestedPermission = "+requestedPermission);
permissionInfos.add(permissionInfo);
requestPermissions.add(requestedPermission);
}
} catch (PackageManager.NameNotFoundException e) {
}
}
for(String permission : requestPermissions){
pkgManager.grantRuntimePermission(mPackageInfo.packageName,
permission, userHandle);
}
}
public boolean isRuntime(int protectionLevel) {
return (protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
== PermissionInfo.PROTECTION_DANGEROUS;
}

public boolean isRuntimeOnly(int protectionLevel) {
return (protectionLevel & PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0;
}

    推荐阅读