PackageManager.getInstalledPackages() 返回空列表

发布于 2024-09-13 10:13:59 字数 325 浏览 11 评论 0原文

使用 PackageManager.getInstalledPackages() 方法时,我遇到了一种非常奇怪的情况。第一次启动活动时,我会获得所有已安装软件包的有效列表。但是当我第二次启动活动时,我得到一个空列表......可能是什么原因造成的?

我正在使用此代码来获取列表: 列表 pkgList = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

我正在针对 1.6 SDK 进行构建,并兼容 1.5+

提前感谢您的任何建议/帮助...我对原因感到非常困惑,并且无法想到我做错了什么。

I am running into a very strange situation when using the PackageManager.getInstalledPackages() method. The first time I launch my activity I get a valid list of all the installed packages. But the second time I launch my activity I get an empty list... What could possibly be causing this?

I am using this code to get the list:
List pkgList = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);

I am building against the 1.6 SDK with compatibility for 1.5+

Thanks in advance for any suggestions/help... I'm really baffled as to the cause and can't think of what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-09-20 10:13:59

由于某种原因,也许需要在主应用程序线程上调用PackageManager。我还没有尝试从 AsyncTask 使用它。

更新 2018-03-26PackageManager 通常可以调用后台线程,尤其是 getInstalledPackages()

Perhaps the PackageManager needs to be invoked on the main application thread, for some reason. I haven't tried using it from an AsyncTask.

UPDATE 2018-03-26: PackageManager generally is fine to invoke on background threads, getInstalledPackages() in particular.

若有似无的小暗淡 2024-09-20 10:13:59

你会得到一个空列表,因为 PackageManager 死了,因为用于返回已安装应用程序列表的 IPC 缓冲区变得大于 1MB 缓冲区大小(从 4.4 开始)。

从 API 15 开始,相同的行为会引发 TransactionTooLargeException。在 <15 个 API 上,仅返回一个空列表,有时在 logcats 中会看到一个小错误。

getInstalledPackages() 不需要在 UI 线程上执行。看起来可能是这样,但这只是偶然的。如果仅在主线程上执行它,则会产生副作用,即它会阻止可能填满进程范围共享 IPC 缓冲区的同时调用。

You are getting an empty list back, because the PackageManager died, because the IPC buffer that is used to return the list of installed apps grew larger than than it's 1MB buffer size (as of 4.4).

Since API 15, the same behavior would throw a TransactionTooLargeException. On <15 APIs just an empty list is returned though and sometimes a small error is visible in logcats.

getInstalledPackages() does not need to be executed on the UI thread. It can seem that way, but this would only be incidental. If you only execute it on the main thread you have the side-effect that it prevents simultaneous calls that could fill up the process-wide shared IPC buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文