ApplicationInfo adapter in Android to display only apps with launcher intent

1.6k views Asked by At

as per title i'm looking for a solution to display only the application with launcher intent. Here is my code to retrieve the app list:

public static List<ApplicationInfo> getInstalledApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
    Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
    return apps;
}

Is it possible to add a rule to remove all the packages without launcher intent? Thanks

2

There are 2 answers

0
iGio90 On BEST ANSWER

I managed to solved by myself using a pices of code provided by marcus.ramsden:

public static List<ApplicationInfo> getInstalledApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
    List<ApplicationInfo> appInfoList = new ArrayList();
    for (ApplicationInfo info : apps) {
        if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
            appInfoList.add(info);
        }
    }
    Collections.sort(appInfoList, new ApplicationInfo.DisplayNameComparator(packageManager));
    return appInfoList;
}
2
marcus.ramsden On

You could use queryIntentActivities();

Intent intent = new Intent()
    .setAction(Intent.ACTION_MAIN)
    .addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
List<ApplicationInfo> appInfoList = new ArrayList<>();
for (ResolveInfo info : list) {
    ApplicationInfo appInfo = packageManager.getApplicationInfo(info.activityInfo.packageName, PackageManager.GET_META_DATA);
    appInfoList.add(appInfo);
}
Collections.sort(appInfoList, new ApplicationInfo.DisplayNameComparator(packageManager));

That should give you all of the intents with a Launcher activity. Note the MATCH_DEFAULT_ONLY should ensure that you only get activities flagged as the default launcher activity for the app.

EDIT: Be careful with MATCH_DEFAULT_ONLY. It might decrease the number of installed apps you get.