在Android 11上调用getPackageInfo时出现NameNotFoundException [英] NameNotFoundException when calling getPackageInfo on Android 11

查看:1289
本文介绍了在Android 11上调用getPackageInfo时出现NameNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 targetSdkVersion 设置为 30 (Android 11)之后,在执行 android.content.pm.PackageManager $ NameNotFoundException 在我知道的软件包上> packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS).

After setting targetSdkVersion to 30 (Android 11) I'm getting android.content.pm.PackageManager$NameNotFoundException when doing packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS) on packages that I know exists.

堆栈跟踪如下:

android.content.pm.PackageManager$NameNotFoundException: com.google.android.apps.maps
        at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:202)
        at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:174)

推荐答案

https中所述://developer.android.com/preview/privacy/package-visibility :

Android 11更改了应用程序查询和与其他应用程序交互的方式用户已安装在设备上的信息.使用新的元素,应用可以定义他们可以访问的其他应用的集合.此元素有助于通过以下方式鼓励最小特权原则:告诉系统哪些其他应用对您的应用可见,以及它可以帮助Google Play等应用商店评估隐私和安全性您的应用程序为用户提供的信息.

Android 11 changes how apps can query and interact with other apps that the user has installed on a device. Using the new element, apps can define the set of other apps that they can access. This element helps encourage the principle of least privilege by telling the system which other apps to make visible to your app, and it helps app stores like Google Play assess the privacy and security that your app provides for users.

如果您的应用定位到Android 11,则可能需要添加应用清单文件中的元素.在元素中,您可以通过程序包名称或意图签名指定应用程序.

If your app targets Android 11, you might need to add the element in your app's manifest file. Within the element, you can specify apps by package name or by intent signature.

因此,您要么必须停止正在做的事情,要么请求访问有关某些软件包的信息,或者-如果有理由,请使用权限

So you either have to stop what you are doing, or request to access information about certain packages, or - if you have reasons for it - use the permission QUERY_ALL_PACKAGES.

要查询特定软件包并与之交互,您可以像这样更新 AndroidManifest.xml :

To query and interact with specific packages you would update your AndroidManifest.xml like this:

<manifest ...>
    ...
    <queries>
        <package android:name="com.example.store" />
        <package android:name="com.example.services" />
    </queries>
    ...
     <application ...>
    ...
</manifest>

查询所有应用并与之交互

我有一个应用程序,需要能够询问所有应用程序的信息.您所要做的就是将以下内容添加到 AndroidManifest.xml :

<manifest ...>
     ...
     <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
     ...
     <application ...>
     ...
</manifest>

这篇关于在Android 11上调用getPackageInfo时出现NameNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆