2

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.

Justin
  • 6,564
  • 6
  • 37
  • 34
  • Ok.... so this appears to be a problem with using AsyncTask. When I take my code out of AsyncTask then PackageManager.getInstalledPackages() correctly returns the list of packages every time... So, any ideas on why this call doesn't work with AsyncTask? – Justin Aug 11 '10 at 06:58

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That is the only thing I could think of... If that is the case though, it should probably be documented. I at least have a workaround, even though my initialization takes a little longer than I would like because I have to get the list of installed applications on the main thread. – Justin Aug 11 '10 at 21:46
  • @Justin: I wouldn't expect `getInstalledPackages()` itself to take very long, though perhaps it does. You could do that in `onPreExecute()` of your `AsyncTask` and the rest of the initialization work in `doInBackground()`. With respect to docs, I agree -- if you can create a sample project demonstrating the problem, post an issue to http://b.android.com with the sample and ask for a documentation update or a bug fix (in case it *should* work on a background thread). – CommonsWare Aug 11 '10 at 21:51
  • I forgot about the onPreExecute() method. I'll try using that and seeing what behavior I get. After your comment on the getInstalledPackages, I think you are right... the slowdown must be somewhere else. I'll see what I can do to get a sample project together. Thanks for the help. – Justin Aug 11 '10 at 21:56
1

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.

darken
  • 807
  • 12
  • 25