10

Im working on a app where I want to present the user with all installed apps and let him/her choose one and then do something with it. I followed a tutorial (this: http://impressive-artworx.de/2011/list-all-installed-apps-in-style/ ) although I'm having some issues. After following the tutorial I only got apps that weren't preinstalled (like all background apps that aren't launchable) which is great if you want the apps that the user has downloaded from the play store. The problem is that in my app I want to display the launchable system apps like Youtube and Browser but not the non-launchable ones like Search Application Provider.

Here's the code that I'm using when to get the apps:

private List<App> loadInstalledApps(boolean includeSysApps) {
  List<App> apps = new ArrayList<App>();

  // the package manager contains the information about all installed apps
  PackageManager packageManager = getPackageManager();

  List<PackageInfo> packs = packageManager.getInstalledPackages(0); //PackageManager.GET_META_DATA 

  for(int i=0; i < packs.size(); i++) {
     PackageInfo p = packs.get(i);
     ApplicationInfo a = p.applicationInfo;

     App app = new App();
     app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
     app.setPackageName(p.packageName);
     app.setVersionName(p.versionName);
     app.setVersionCode(p.versionCode);
     CharSequence description = p.applicationInfo.loadDescription(packageManager);
     app.setDescription(description != null ? description.toString() : "");
     apps.add(app);
  }
  return apps;
  }

Now my question is; what is the best way to filter out the non-launchable apps?

Any help is appreciated!

SweSnow
  • 17,504
  • 10
  • 36
  • 49

1 Answers1

26

The Best way is:

public static List<ApplicationInfo> getAllInstalledApplications(Context context) {
    List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(PackageManager.PERMISSION_GRANTED);
    List<ApplicationInfo> launchableInstalledApps = new ArrayList<ApplicationInfo>();
    for(int i =0; i<installedApps.size(); i++){
        if(context.getPackageManager().getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
            //If you're here, then this is a launch-able app
            launchableInstalledApps.add(installedApps.get(i));
        }
    }
    return launchableInstalledApps;
}
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • 1
    This is not a good idea, because some apps return null for getLaunchIntentForPackageName. With this solution I had the problem that the "Telephony" app was not in the list of apps. For a better solution look here: https://stackoverflow.com/a/30446616/4274651 –  Dec 11 '17 at 11:52