溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android9.0中怎么添加默認(rèn)apk權(quán)限

發(fā)布時(shí)間:2021-06-11 11:26:14 來(lái)源:億速云 閱讀:527 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

android9.0中怎么添加默認(rèn)apk權(quán)限?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

1.默認(rèn)賦予全部權(quán)限:
安卓動(dòng)態(tài)要求用戶允許添加權(quán)限,直接將如下代碼中的final boolean grantPermissions = (args.installFlags
& PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0;改為final boolean grantPermissions=true便可。

文件路徑:framework\base\services\core\java\com\android\server\pm\PackageManagerService.java

case POST_INSTALL: {
                    if (DEBUG_INSTALL) Log.v(TAG, "Handling post-install for " + msg.arg1);

                    PostInstallData data = mRunningInstalls.get(msg.arg1);
                    final boolean didRestore = (msg.arg2 != 0);
                    mRunningInstalls.delete(msg.arg1);

                    if (data != null) {
                        InstallArgs args = data.args;
                        PackageInstalledInfo parentRes = data.res;
                        
                        /*final boolean grantPermissions = (args.installFlags
                                & PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0;
                        */
                        //直接將grantPermissions該為true便可
                        final boolean grantPermissions = true;
                        final boolean killApp = (args.installFlags
                                & PackageManager.INSTALL_DONT_KILL_APP) == 0;
                        final boolean virtualPreload = ((args.installFlags
                                & PackageManager.INSTALL_VIRTUAL_PRELOAD) != 0);
                        final String[] grantedPermissions = args.installGrantPermissions;

                        // Handle the parent package
                        handlePackagePostInstall(parentRes, grantPermissions, killApp,
                                virtualPreload, grantedPermissions, didRestore,
                                args.installerPackageName, args.observer);

                        // Handle the child packages
                        final int childCount = (parentRes.addedChildPackages != null)
                                ? parentRes.addedChildPackages.size() : 0;
                        for (int i = 0; i < childCount; i++) {
                            PackageInstalledInfo childRes = parentRes.addedChildPackages.valueAt(i);
                            handlePackagePostInstall(childRes, grantPermissions, killApp,
                                    virtualPreload, grantedPermissions, false /*didRestore*/,
                                    args.installerPackageName, args.observer);
                        }

                        // Log tracing if needed
                        if (args.traceMethod != null) {
                            Trace.asyncTraceEnd(TRACE_TAG_PACKAGE_MANAGER, args.traceMethod,
                                    args.traceCookie);
                        }
                    } else {
                        Slog.e(TAG, "Bogus post-install token " + msg.arg1);
                    }

                    Trace.asyncTraceEnd(TRACE_TAG_PACKAGE_MANAGER, "postInstall", msg.arg1);
                } break;

2.根據(jù)包名賦予權(quán)限:
文件路徑:framework\base\services\core\java\com\android\server\pm\permission\DefaultPermissionGrantPolicy.java

--- a/frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java
+++ b/frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java
@@ -94,7 +94,7 @@ import java.util.Set;
  */
 public final class DefaultPermissionGrantPolicy {
     private static final String TAG = "DefaultPermGrantPolicy"; // must be <= 23 chars
-    private static final boolean DEBUG = false;
+    private static final boolean DEBUG = true;

     private static final int DEFAULT_FLAGS =
             PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
@@ -216,6 +216,7 @@ public final class DefaultPermissionGrantPolicy {
                 }
             }
         };
+        Slog.e(TAG,"vsoonbsp helloworld\n");
         mPermissionGrantedCallback = callback;
         mPermissionManager = permissionManager;
         mServiceInternal = LocalServices.getService(PackageManagerInternal.class);
@@ -263,8 +264,55 @@ public final class DefaultPermissionGrantPolicy {
         }
     }

+    //聲明要賦予apk包名
+    private static final String PCK_NAME_MICROSOFT = "com.microsoft.translator";
+    private static final String PCK_NAME_TTS = "com.google.android.tts";
+    private static final String PCK_NAME_SEARCH = "com.google.android.googlequicksearchbox";
+

+    private void grantPermissionsToCustomApp(int userId){
+        try{
+            PackageParser.Package customPackage = getSystemPackage(PCK_NAME_MICROSOFT);
+            if ((customPackage != null) && doesPackageSupportRuntimePermissions(customPackage)) {
+                grantRuntimePermissions(customPackage, LOCATION_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, STORAGE_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, MICROPHONE_PERMISSIONS, userId);
+            }
+
+            customPackage = getSystemPackage(PCK_NAME_MICROSOFT);
+            if ((customPackage != null) && doesPackageSupportRuntimePermissions(customPackage)) {
+                grantRuntimePermissions(customPackage, LOCATION_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, STORAGE_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, MICROPHONE_PERMISSIONS, userId);
+            }
+
+            customPackage = getSystemPackage(PCK_NAME_SEARCH);
+            if ((customPackage != null) && doesPackageSupportRuntimePermissions(customPackage)) {
+                grantRuntimePermissions(customPackage, LOCATION_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, STORAGE_PERMISSIONS, userId);
+                grantRuntimePermissions(customPackage, MICROPHONE_PERMISSIONS, userId);
+            }
+
+
+        }catch(Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     public void grantDefaultPermissions(int userId) {
         grantPermissionsToSysComponentsAndPrivApps(userId);
+        grantPermissionsToCustomApp(userId);
         grantDefaultSystemHandlerPermissions(userId);
         grantDefaultPermissionExceptions(userId);
     }

3.通過(guò)default-permissions-google.xml的方式
該文件編譯的時(shí)候要編譯到/system/etc/default-permissions/default-permissions-google.xml中。
可以使用Android.mk的PRODUCT_COPY_FILES方式拷貝。
關(guān)于default-permissions-google.xml相關(guān)的可以參考谷歌https://source.android.google.cn/devices/tech/config/perms-whitelist

<exception
            package="com.google.android.tts"
            sha256-cert-digest="56:BE:13:2B:78:06:56:FE:24:44:CD:34:32:6E:B5:D7:AA:C9:1D:20:96:AB:F0:FE:67:3A:99:27:06:22:EC:87">
        <!-- External storage -->
        <permission name="android.permission.READ_EXTERNAL_STORAGE" fixed="false"/>
        <permission name="android.permission.WRITE_EXTERNAL_STORAGE" fixed="false"/>
        <!-- Contacts -->
        <permission name="android.permission.READ_CONTACTS" fixed="false"/>
        <permission name="android.permission.WRITE_CONTACTS" fixed="false"/>
        <!-- microphone -->
        <permission name="android.permission.RECORD_AUDIO" fixed="false"/>
    </exception>

   <exception
            package="com.google.android.googlequicksearchbox"
            sha256-cert-digest="56:BE:13:2B:78:06:56:FE:24:44:CD:34:32:6E:B5:D7:AA:C9:1D:20:96:AB:F0:FE:67:3A:99:27:06:22:EC:87">
        <!-- External storage -->
        <permission name="android.permission.READ_EXTERNAL_STORAGE" fixed="false"/>
        <permission name="android.permission.WRITE_EXTERNAL_STORAGE" fixed="false"/>
        <!-- Contacts -->
        <permission name="android.permission.READ_CONTACTS" fixed="false"/>
        <permission name="android.permission.WRITE_CONTACTS" fixed="false"/>
        <!-- microphone -->
        <permission name="android.permission.RECORD_AUDIO" fixed="false"/>
    </exception>

   <exception
            package="com.microsoft.translator"
            sha256-cert-digest="56:BE:13:2B:78:06:56:FE:24:44:CD:34:32:6E:B5:D7:AA:C9:1D:20:96:AB:F0:FE:67:3A:99:27:06:22:EC:87">
        <!-- External storage -->
        <permission name="android.permission.READ_EXTERNAL_STORAGE" fixed="false"/>
        <permission name="android.permission.WRITE_EXTERNAL_STORAGE" fixed="false"/>
        <!-- Contacts -->
        <permission name="android.permission.READ_CONTACTS" fixed="false"/>
        <permission name="android.permission.WRITE_CONTACTS" fixed="false"/>
        <!-- microphone -->
        <permission name="android.permission.RECORD_AUDIO" fixed="false"/>
    </exception>

看完上述內(nèi)容,你們掌握android9.0中怎么添加默認(rèn)apk權(quán)限的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI