Android 默認(rèn)打開應(yīng)用權(quán)限

[TOC]

問題描述

系統(tǒng)預(yù)裝APK和后安裝的APK,默認(rèn)全部打開應(yīng)用,不彈窗提醒!,不彈窗提醒!不彈窗提醒!

解決方法

1、系統(tǒng)預(yù)裝應(yīng)用

W:\Code\RK3399PRO_HDMI_ANDROID8\frameworks\base\services\core\java\com\android\server\pm\DefaultPermissionGrantPolicy.java文件內(nèi)的

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);
            }

說明:如果有其它預(yù)裝應(yīng)用程序,以此類推

2、后安裝的應(yīng)用

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);
            }

源碼中默認(rèn)的為grantPermissions,可根據(jù)需求增加自己的應(yīng)用,該修改為所有應(yīng)用都打開權(quán)限

Android 9 平臺(tái)

在Android 9平臺(tái)上,權(quán)限結(jié)構(gòu)已改變,除以上修改外,還要加上以下修改:

frameworks\base\services\core\java\com\android\server\pm\permission\PermissionManagerService.java文件內(nèi),修改

private void grantPermissions(PackageParser.Package pkg, boolean replace,
            String packageOfInterest, PermissionCallback callback)

函數(shù)部分為:

增加如下:

if (bp != null && permissionsState.grantInstallPermission(bp) !=
                PermissionsState.PERMISSION_OPERATION_FAILURE) {
                    changedInstallPermission = true;
                }

意為:只要要獲取的權(quán)限沒啥問題,就通過!changedInstallPermission = true;

以上方法針對(duì)危險(xiǎn)權(quán)限((Dangerous Permission)),但還有其他系統(tǒng)權(quán)限權(quán)限想要默認(rèn)開啟無法用此方法。

PACKAGE_USAGE_STATS權(quán)限

image

? 該權(quán)限可使應(yīng)用統(tǒng)計(jì)其他應(yīng)用使用情況,想要默認(rèn)開啟

? 先在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權(quán)限

image

? 該權(quán)限可用于修改系統(tǒng)設(shè)置,如修改系統(tǒng)亮度等。

? 需要默認(rèn)開啟該權(quán)限,需使用系統(tǒng)簽名

? 先在AndroidMainfests.xml中增加

<uses-permission android:name="android.permission.WRITE_CONTACTS" />

? 和

android:sharedUserId="android.uid.system"

? 再在該預(yù)裝應(yīng)用的Android.mk中增加如下

LOCAL_CERTIFICATE := platform

最后編譯系統(tǒng)燒錄固件即可。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容