Android: How to get a list of installed activities, as they appear in launcher, without duplicates
Intent localIntent2 = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent localIntent3 = new Intent(Intent.ACTION_MAIN, null);
localIntent3.addCategory(Intent.CATEGORY_LAUNCHER);
localIntent2.putExtra(Intent.EXTRA_INTENT, localIntent3);
startActivityForResult(localIntent2, 1);
Try this code. It will list out only the applications which are all installed in your device.
I might be late, but i just found a perfect way of getting all apps having launcher & no duplicate apps (including system apps like contacts, maps, etc.). Although, Satheesh's answer might be working (haven't checked myself), but I wanted to pick multiple activities so I used below code to get installed apps.
I used your second approach and discarded duplicate packages using HashSet. Here's the final code :
final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
//using hashset so that there will be no duplicate packages,
//if no duplicate packages then there will be no duplicate apps
HashSet<String> packageNames = new HashSet<String>(0);
List<ApplicationInfo> appInfos = new ArrayList<ApplicationInfo>(0);
//getting package names and adding them to the hashset
for(ResolveInfo resolveInfo : resInfos) {
packageNames.add(resolveInfo.activityInfo.packageName);
}
//now we have unique packages in the hashset, so get their application infos
//and add them to the arraylist
for(String packageName : packageNames) {
try {
appInfos.add(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
} catch (NameNotFoundException e) {
//Do Nothing
}
}
//to sort the list of apps by their names
Collections.sort(appInfos, new ApplicationInfo.DisplayNameComparator(packageManager));
Try below code and let me know what happened.
PackageManager manager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos= manager.queryIntentActivities(mainIntent, 0);
// Below line is new code i added to your code
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(manager));
for(ResolveInfo info : resolveInfos) {
ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
//...
//get package name, icon and label from applicationInfo object
}