Question

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

Was it helpful?

Solution 2

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;
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top