0

I tried some of the solutions found here but they all have some kind of errors.

Here's what I got:

final PackageManager packManager = getPackageManager();
List<ApplicationInfo> appsInfo;

appsInfo = packManager.getInstalledApplications(PackageManager.GET_META_DATA);

for ( int i = 0 ; i < appsInfo.size() ; i++ ) {
    if (/*app is not launchable*/) {
       appInfo.remove(i);      
    }
}

What I want to know is:

  1. What is the condition that should go inside that if statement?
  2. Surely there has to be a faster way to get launchable apps other than getting all the apps then looping through them to remove the un-launchable ones? I'm hoping for a statement similar to getInstalledApplications like above except it only returns launchable apps.
  3. Sorry I'm a beginner but what does GET_META_DATA in the above code do and should something else be in its place?
User104163
  • 532
  • 6
  • 17
  • "I tried some of the solutions found here but they all have some kind of errors" -- then perhaps you should have a [mcve] showing what you tried and what the specific problems were that you encountered. If you are trying to build a launcher (or something like that), you need the list of all activities that advertise as belonging in a launcher. You use `queryIntentActivities()`, not `getInstalledApplications()`, for that. – CommonsWare Jun 08 '20 at 21:33
  • 1
    [This sample app](https://github.com/commonsguy/cw-omnibus/blob/v9.0/Introspection/Launchalot/app/src/main/java/com/commonsware/android/launchalot/Launchalot.java) of mine is *ancient*, but it shows presenting a list of activities like a launcher might, including launching the activity if the user clicks on it. – CommonsWare Jun 08 '20 at 21:35
  • @CommonsWare Hi I mean there are comments below those solutions saying the code isn't quite right. – User104163 Jun 08 '20 at 21:41
  • Ah ok so does pm.queryIntentActivities(main, 0) return a list of only launchable apps? – User104163 Jun 08 '20 at 21:43
  • IMHO, "launchable apps" isn't really a thing in Android -- at the very least, I do not know what your definition of that phrase is. `queryIntentActivities()` returns details of activities that apps advertise as belonging in a launcher. Any app can advertise zero, one, or several such activities. A launcher usually displays all of those activities in a list, grid, or similar UI structure. – CommonsWare Jun 08 '20 at 21:46
  • By launchable I mean when the user opens his app drawer he can find the app there and launch it :) – User104163 Jun 08 '20 at 21:51
  • An Android launcher's "app drawer" does not show apps. It shows activities. As I wrote in my previous comment, `queryIntentActivities()` returns details of activities that apps advertise as belonging in a launcher (e.g., in an "app drawer"). Any app can advertise zero, one, or several such activities. A launcher usually displays all of those activities in a list, grid, or similar UI structure (e.g., in an "app drawer"). – CommonsWare Jun 08 '20 at 21:54
  • thanks I think that means they're launchable. You should put the answer in a post so I can give it a check. – User104163 Jun 08 '20 at 22:04
  • 1
    This topic has been covered quite a bit before -- I added a few duplicate questions. In terms of `GET_META_DATA`, this will give you access to the contents of any `` elements inside the `` element of the manifest entries for the matched activities. There may be cases where an app launcher would need that, but I cannot think of any off the top of my head. – CommonsWare Jun 08 '20 at 22:13
  • Hi sorry I just have one more question, is it necessary to write "Intent main=new Intent(Intent.ACTION_MAIN, null);" and "main.addCategory(Intent.CATEGORY_LAUNCHER);" to use queryIntentActivities() in this case? – User104163 Jun 08 '20 at 22:22
  • 1
    If your objective is to get a list of activities that belong in a launcher, you need an `Intent` with `ACTION_MAIN` and `CATEGORY_LAUNCHER` to use with `queryIntentActivities()`. The sample app that I linked to in my second comment is covered in one of my books, including in [the free older editions](https://commonsware.com/Android/4-2-free). Search for "Launchalot". In Version 7.4 of the book (the most recent free one), that app is covered in the "PackageManager Tricks" chapter, with a few pages of details. – CommonsWare Jun 08 '20 at 22:25

0 Answers0