Android12 授予APK默认权限

不同于以往的Android版本 可以直接在此处设置:

Android/frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java

java 复制代码
private void grantDefaultSystemHandlerPermissions(PackageManagerWrapper pm, int userId) {
        Log.i(TAG, "Granting permissions to default platform handlers for user " + userId);

        ..........省略代码



    
        // Installer
        grantSystemFixedPermissionsToSystemPackage(pm,
                ArrayUtils.firstOrNull(getKnownPackages(
                        PackageManagerInternal.PACKAGE_INSTALLER, userId)),
                userId, STORAGE_PERMISSIONS);

        // Verifier
        final String verifier = ArrayUtils.firstOrNull(getKnownPackages(
                PackageManagerInternal.PACKAGE_VERIFIER, userId));
        grantSystemFixedPermissionsToSystemPackage(pm, verifier, userId, STORAGE_PERMISSIONS);
        grantPermissionsToSystemPackage(pm, verifier, userId, PHONE_PERMISSIONS, SMS_PERMISSIONS);

        // SetupWizard
        grantPermissionsToSystemPackage(pm,
                ArrayUtils.firstOrNull(getKnownPackages(
                        PackageManagerInternal.PACKAGE_SETUP_WIZARD, userId)), userId,
                PHONE_PERMISSIONS, CONTACTS_PERMISSIONS, ALWAYS_LOCATION_PERMISSIONS,
                CAMERA_PERMISSIONS);

        // Camera
        grantPermissionsToSystemPackage(pm,
                getDefaultSystemHandlerActivityPackage(pm, MediaStore.ACTION_IMAGE_CAPTURE, userId),
                userId, CAMERA_PERMISSIONS, MICROPHONE_PERMISSIONS, STORAGE_PERMISSIONS);
    

        // Sound recorder
        grantPermissionsToSystemPackage(pm,
                getDefaultSystemHandlerActivityPackage(pm,
                        MediaStore.Audio.Media.RECORD_SOUND_ACTION, userId),
                userId, MICROPHONE_PERMISSIONS);

        // Media provider
        grantSystemFixedPermissionsToSystemPackage(pm,
                getDefaultProviderAuthorityPackage(MediaStore.AUTHORITY, userId), userId,
                STORAGE_PERMISSIONS);

        // Downloads provider
        grantSystemFixedPermissionsToSystemPackage(pm,
                getDefaultProviderAuthorityPackage("downloads", userId), userId,
                STORAGE_PERMISSIONS);

        // Downloads UI
        grantSystemFixedPermissionsToSystemPackage(pm,
                getDefaultSystemHandlerActivityPackage(pm,
                        DownloadManager.ACTION_VIEW_DOWNLOADS, userId),
                userId, STORAGE_PERMISSIONS);

        // Storage provider
        grantSystemFixedPermissionsToSystemPackage(pm,
                getDefaultProviderAuthorityPackage("com.android.externalstorage.documents", userId),
                userId, STORAGE_PERMISSIONS);

        // CertInstaller
        grantSystemFixedPermissionsToSystemPackage(pm,
                getDefaultSystemHandlerActivityPackage(pm, Credentials.INSTALL_ACTION, userId),
                userId, STORAGE_PERMISSIONS);

授予权限类型参考下表:

比如 为DemoAPK添加定位&通话以及访问内存权限.

java 复制代码
        // DemoAPK
        grantPermissionsToSystemPackage(pm, "com.test.demoApk",
                userId, PHONE_PERMISSIONS, ALWAYS_LOCATION_PERMISSIONS, STORAGE_PERMISSIONS);
相关推荐
二流小码农36 分钟前
鸿蒙开发:自定义一个简单的标题栏
android·ios·harmonyos
kinnisoy39 分钟前
Zotero 7本地pdf文件名自适应中英文格式
android·java·pdf
雪芽蓝域zzs2 小时前
Android Studio 控制台输出的中文显示乱码
android·ide·android studio
冷白白2 小时前
【MySQL】函数
android·数据库·mysql·adb
闲暇部落3 小时前
Android OpenGL ES详解——实例化
android·opengl·实例化·实例化数组·小行星带
The_tuber_sadness3 小时前
Kotlin深度面试题:协程、密封类和高阶函数
android·开发语言·kotlin
城市牛马人3 小时前
ForEach刷新UI机制
android
清源妙木真菌7 小时前
Linux:进程状态
android·linux·网络
kigumi8 小时前
Android Framework与JNI
android·java
懒大王敲代码9 小时前
快速搭建Android开发环境:Docker部署docker-android并实现远程连接
android·docker·容器