if ("com.jxw.online_study".equals(packageInfo.packageName)) { if (mAppPermissions.getPermissionGroups().isEmpty()) { return; } for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) { String[] permissionsToGrant = null; final int permissionCount = group.getPermissions().size(); for (int j = 0; j < permissionCount; j++) { final Permission permission = group.getPermissions().get(j); permissionsToGrant = ArrayUtils.appendString( permissionsToGrant, permission.getName()); } if (permissionsToGrant != null) { group.grantRuntimePermissions(true, false, permissionsToGrant); } } confirmPermissionsReview(); executeCallback(true); activity.finishAfterTransition(); }优化这段代码,添加多个包名
时间: 2023-06-12 21:08:24 浏览: 369
简单的Jxl.jar 解析Excel
可以将这段代码封装成一个方法,传入包名和权限组列表参数,然后在调用时传入多个包名和对应的权限组列表即可。
代码示例:
```
private void grantRuntimePermissions(String packageName, List<AppPermissionGroup> permissionGroups) {
if (TextUtils.isEmpty(packageName) || permissionGroups == null || permissionGroups.isEmpty()) {
return;
}
for (AppPermissionGroup group : permissionGroups) {
String[] permissionsToGrant = null;
final int permissionCount = group.getPermissions().size();
for (int j = 0; j < permissionCount; j++) {
final Permission permission = group.getPermissions().get(j);
permissionsToGrant = ArrayUtils.appendString(permissionsToGrant, permission.getName());
}
if (permissionsToGrant != null) {
group.grantRuntimePermissions(true, false, permissionsToGrant);
}
}
confirmPermissionsReview();
executeCallback(true);
activity.finishAfterTransition();
}
```
调用示例:
```
grantRuntimePermissions("com.jxw.online_study", permissionGroups1);
grantRuntimePermissions("com.example.app1", permissionGroups2);
grantRuntimePermissions("com.example.app2", permissionGroups3);
```
阅读全文