android9.0 默认apk权限添加方法

 更新时间:2021年06月11日 10:31:45   作者:Ian22l  
本文给大家分享android9.0 默认apk权限添加方法,默认赋予全部权限,根据包名赋予权限,通过default-permissions-google.xml的方式实现,文中通过实例代码给大家介绍的非常详细,需要的朋友参考下吧

1.默认赋予全部权限:
安卓动态要求用户允许添加权限,直接将如下代码中的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.根据包名赋予权限:
文件路径: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.通过default-permissions-google.xml的方式
该文件编译的时候要编译到/system/etc/default-permissions/default-permissions-google.xml中。
可以使用Android.mk的PRODUCT_COPY_FILES方式拷贝。
关于default-permissions-google.xml相关的可以参考谷歌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>

以上就是android9.0 默认apk权限添加的详细内容,更多关于android9.0 添加默认权限的资料请关注脚本之家其它相关文章!

相关文章

  • Android控件拖动实例详解

    Android控件拖动实例详解

    这篇文章主要介绍了 Android控件的拖动实例详解的相关资料,这里附有实例代码,帮助大家学习理解,需要的朋友可以参考下
    2016-12-12
  • Android客户端实现注册、登录详解(2)

    Android客户端实现注册、登录详解(2)

    这篇文章主要为大家详细介绍了Android客户端实现注册、登录代码第二篇,App与服务器的交互实现登录和自动登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android登录界面的实现代码分享

    Android登录界面的实现代码分享

    好久没有搞android项目了,手都有点松了,今天因为项目的需要,继续弄android知识,在项目中登录界面是项目中比较常见的最基本的功能,对android登录界面的实现感兴趣的朋友一起学习吧
    2016-11-11
  • Android实现头像上传功能

    Android实现头像上传功能

    这篇文章主要为大家详细介绍了Android实现头像上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Android Activity 横竖屏切换的生命周期

    Android Activity 横竖屏切换的生命周期

    这篇文章主要介绍了Android Activity 横竖屏切换的生命周期的相关资料,需要的朋友可以参考下
    2016-04-04
  • Android通用索引栏实现代码

    Android通用索引栏实现代码

    这篇文章主要为大家详细介绍了Android通用索引栏实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • Android 中的注解详细介绍

    Android 中的注解详细介绍

    本文主要介绍 Android 中的注解,这里整理详细的资料来说明Android注解是如何使用,提高编程效率,有兴趣的小伙伴可以参考下
    2016-08-08
  • Android开发之ListView列表刷新和加载更多实现方法

    Android开发之ListView列表刷新和加载更多实现方法

    这篇文章主要介绍了Android开发之ListView列表刷新和加载更多实现方法,实例分析了ListView列表操作的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 浅谈Android面向切面编程(AOP)

    浅谈Android面向切面编程(AOP)

    这篇文章主要介绍了浅谈Android面向切面编程(AOP),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Android  LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate()详解及分析

    这篇文章主要介绍了Android LayoutInflater.inflate()详解及分析的相关资料,需要的朋友可以参考下
    2017-01-01

最新评论