android 6.0 开始引入动态权限机制,某些敏感权限需要用户手动同意后才可以使用。
作为系统APP ,无需动态申请,声明后直接使用即可。
如果第三方APP需要默认拥有某些权限,无需动态申请,该怎么做呢?
两种方法:
1.第三方APP打系统签名;
2.修改源码,默认授予第三方APP权限。
基于AN 8.0 ,默认权限的管理在 frameworks/base/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java
类,首次开机会执行,我们关注grantDefaultSystemHandlerPermissions(int userId)
函数,
synchronized (mService.mPackages) {
// Installer
PackageParser.Package installerPackage = getSystemPackageLPr(
mService.mRequiredInstallerPackage);
if (installerPackage != null
&& doesPackageSupportRuntimePermissions(installerPackage)) {
grantRuntimePermissionsLPw(installerPackage, STORAGE_PERMISSIONS, true, userId);
}
// Verifier
PackageParser.Package verifierPackage = getSystemPackageLPr(
mService.mRequiredVerifierPackage);
if (verifierPackage != null
&& doesPackageSupportRuntimePermissions(verifierPackage)) {
grantRuntimePermissionsLPw(verifierPackage, STORAGE_PERMISSIONS, true, userId);
grantRuntimePermissionsLPw(verifierPackage, PHONE_PERMISSIONS, false, userId);
grantRuntimePermissionsLPw(verifierPackage, SMS_PERMISSIONS, false, userId);
}
// SetupWizard
PackageParser.Package setupPackage = getSystemPackageLPr(
mService.mSetupWizardPackage);
if (setupPackage != null
&& doesPackageSupportRuntimePermissions(setupPackage)) {
grantRuntimePermissionsLPw(setupPackage, PHONE_PERMISSIONS, userId);
grantRuntimePermissionsLPw(setupPackage, CONTACTS_PERMISSIONS, userId);
grantRuntimePermissionsLPw(setupPackage, LOCATION_PERMISSIONS, userId);
grantRuntimePermissionsLPw(setupPackage, CAMERA_PERMISSIONS, userId);
}
// Camera
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
PackageParser.Package cameraPackage = getDefaultSystemHandlerActivityPackageLPr(
cameraIntent, userId);
if (cameraPackage != null
&& doesPackageSupportRuntimePermissions(cameraPackage)) {
grantRuntimePermissionsLPw(cameraPackage, CAMERA_PERMISSIONS, userId);
grantRuntimePermissionsLPw(cameraPackage, MICROPHONE_PERMISSIONS, userId);
grantRuntimePermissionsLPw(cameraPackage, STORAGE_PERMISSIONS, userId);
}
//省略部分代码
mService.mSettings.onDefaultRuntimePermissionsGrantedLPr(userId);
}
以 Camera 为例,通过 grantRuntimePermissionsLPw
函数授予其相关的权限,权限的定义为
private static final Set<String> MICROPHONE_PERMISSIONS = new ArraySet<>();
static {
MICROPHONE_PERMISSIONS.add(Manifest.permission.RECORD_AUDIO);
}
private static final Set<String> CAMERA_PERMISSIONS = new ArraySet<>();
static {
CAMERA_PERMISSIONS.add(Manifest.permission.CAMERA);
}
private static final Set<String> STORAGE_PERMISSIONS = new ArraySet<>();
static {
STORAGE_PERMISSIONS.add(Manifest.permission.READ_EXTERNAL_STORAGE);
STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
如此,参考 Camera ,给第三方应用授予相关权限:
private static final String MYAPPE_PKG_NAME= "com.myapp.test";
// test app
PackageParser.Package myAppPackage = getPackageLPr(MYAPPE_PKG_NAME);
if(null != fxHomePackage
&& doesPackageSupportRuntimePermissions(fxHomePackage)){
grantRuntimePermissionsLPw(fxHomePackage, CAMERA_PERMISSIONS, userId);
grantRuntimePermissionsLPw(fxHomePackage, CONTACTS_PERMISSIONS, userId);
grantRuntimePermissionsLPw(fxHomePackage, LOCATION_PERMISSIONS, userId);
grantRuntimePermissionsLPw(fxHomePackage, MICROPHONE_PERMISSIONS, userId);
grantRuntimePermissionsLPw(fxHomePackage, STORAGE_PERMISSIONS, userId);
Log.d(TAG, "Granting permissions to package com.bestv.ott");
}
烧录后查看 /data/system/users/0/runtime-permissions.xml
文件,
通过包名查找
应用的权限授予情况,
当然,代码逻辑及功能检查更稳妥。
参考:https://blog.csdn.net/fmc088/article/details/81050520