问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/3455781/packagemanager-getinstalledpackages-returns-empty-list