[TOC]
问题描述
系统预装APK和后安装的APK,默认全部打开应用,不弹窗提醒!,不弹窗提醒!,不弹窗提醒!。
解决方法
1、系统预装应用
W:\Code\RK3399PRO_HDMI_ANDROID8\frameworks\base\services\core\java\com\android\server\pm\DefaultPermissionGrantPolicy.java
文件内的
private void grantDefaultSystemHandlerPermissions(int userId)
方法中增加如下代码:
synchronized (mService.mPackages) {
//以下代码为增加的部分
PackageParser.Package EV_DesktopPckg = getPackageLPr( "com.example.desktop");
if (EV_DesktopPckg != null) {
grantRuntimePermissionsLPw(EV_DesktopPckg, MICROPHONE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, STORAGE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, CONTACTS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, PHONE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, CALENDAR_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, CAMERA_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, LOCATION_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, SENSORS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, SMS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_DesktopPckg, STORAGE_PERMISSIONS, true, userId);
}
PackageParser.Package EV_WebViewTestPckg = getPackageLPr( "com.example.webviewtest");
if (EV_WebViewTestPckg != null) {
grantRuntimePermissionsLPw(EV_WebViewTestPckg, MICROPHONE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, STORAGE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, CONTACTS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, PHONE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, CALENDAR_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, CAMERA_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, LOCATION_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, SENSORS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, SMS_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(EV_WebViewTestPckg, STORAGE_PERMISSIONS, true, userId);
}
说明:如果有其它预装应用程序,以此类推
2、后安装的应用
在W:\Code\RK3399PRO_HDMI_ANDROID8\frameworks\base\services\core\java\com\android\server\pm\PackageManagerService.java
的
private void handlePackagePostInstall(PackageInstalledInfo res, boolean grantPermissions,
boolean killApp, boolean virtualPreload, String[] grantedPermissions,
boolean launchedForRestore, String installerPackage,
IPackageInstallObserver2 installObserver)
其中部分代码修改如下即可
// 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(true
//if (grantPermissions
// ||
// res.name.equals("com.example.desktop") ||
// res.name.equals("com.example.webviewtest") ||
// res.name.equals("com.example.desktop")
) {
grantRequestedRuntimePermissions(res.pkg, res.newUsers, grantedPermissions);
}
源码中默认的为grantPermissions
,可根据需求增加自己的应用,该修改为所有应用都打开权限
Android 9 平台
在Android 9平台上,权限结构已改变,除以上修改外,还要加上以下修改:
在frameworks\base\services\core\java\com\android\server\pm\permission\PermissionManagerService.java文件内,修改
private void grantPermissions(PackageParser.Package pkg, boolean replace,
String packageOfInterest, PermissionCallback callback)
函数部分为:
增加如下:
if (bp != null && permissionsState.grantInstallPermission(bp) !=
PermissionsState.PERMISSION_OPERATION_FAILURE) {
changedInstallPermission = true;
}
意为:只要要获取的权限没啥问题,就通过!changedInstallPermission = true;
以上方法针对危险权限((Dangerous Permission)),但还有其他系统权限权限想要默认开启无法用此方法。
PACKAGE_USAGE_STATS权限
该权限可使应用统计其他应用使用情况,想要默认开启
先在AndroidMainfests.xml
中增加
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
再使用ADB命令开启
pm grant com.example.hello android.permission.PACKAGE_USAGE_STATS
WRITE_SETTINGS权限
该权限可用于修改系统设置,如修改系统亮度等。
需要默认开启该权限,需使用系统签名
先在AndroidMainfests.xml
中增加
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
和
android:sharedUserId="android.uid.system"
再在该预装应用的Android.mk
中增加如下
LOCAL_CERTIFICATE := platform
最后编译系统烧录固件即可。