文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

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;
                        
                        
                        //直接将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 ,
                                    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 添加默认权限的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯