Android 默认打开应用的权限
有项目需要客户要安装第三方软件,但是要手动点击打开权限,就想不动手就打开。
//安装第三方软件,修改方式
frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java
//找到如下源码:
//有三种方式,但是只有1 或 3 ,两种方式有用
// Now that we successfully installed the package, grant runtime
// permissions if requested before broadcasting the install. Also
// for legacy apps in permission review mode we clear the permission
// review flag which is used to emulate runtime permissions for
// legacy apps.
if (grantPermissions) {
final int callingUid = Binder.getCallingUid();
mPermissionManager.grantRequestedRuntimePermissions(
res.pkg, res.newUsers, grantedPermissions, callingUid);
}
//如下修改
if (packageName.equals("com.android.test")) {
final int callingUid = Binder.getCallingUid();
//1 该方法打开所有请求的权限
mPermissionManager.grantRequestedRuntimePermissions(
res.pkg, res.newUsers, grantedPermissions, callingUid);
//2 该方法似乎不起作用
//grantRuntimePermission(packageName,"android.permission.SYSTEM_OVERLAY_WINDOW",res.uid);
//grantRuntimePermission(packageName,"android.permission.SYSTEM_ALERT_WINDOW",res.uid);
//grantRuntimePermission(packageName,"android.permission.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION",res.uid);
//3 只打开一个 SYSTEM_ALERT_WINDOW 权限
AppOpsManager mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
mAppOpsManager.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW,
res.uid, packageName,AppOpsManager.MODE_ALLOWED);
}
//内置应用权限修改方式:
frameworks\base\services\core\java\com\android\server\pm\permission\DefaultPermissionGrantPolicy.java
private void grantDefaultSystemHandlerPermissions(PackageManagerWrapper pm, int userId) {
//末尾加入
grantMyPermissions(pm, "com.android.test", userId);
}
private void grantMyPermissions(PackageManagerWrapper pm,String pkg,int userId){ Log.i(TAG,"other package Permissions"); PackageInfo pkginfo = pm.getPackageInfo(pkg); if(pkginfo==null){ return; } Set<String> permissions = new ArraySet<>(); permissions.add("android.permission.SYSTEM_OVERLAY_WINDOW"); permissions.add("android.permission.SYSTEM_ALERT_WINDOW"); permissions.add("android.permission.WRITE_EXTERNAL_STORAGE"); permissions.add("android.permission.READ_EXTERNAL_STORAGE"); grantRuntimePermissions(pm,pkginfo, permissions,false, userId); }