Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter out non-launchable apps when getting all installed apps

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!

like image 653
SweSnow Avatar asked Oct 02 '12 15:10

SweSnow


1 Answers

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;
}
like image 80
Shridutt Kothari Avatar answered Oct 17 '22 09:10

Shridutt Kothari