Hello Friends, We will see how to get list of all installed application from android device programmatically,
We call a method of PackageManager like
It returns a list of packageInfo object. Now we will strore our app list in ArrayList.
As shown below.
Now we have a list, we will just sort our array in alphabetical order using Java Collection API.
For that we require our custom comparator.
Create a Inner static class AlPhabeticalComarator
To store information of each application we require a Class Object Create as follows
Create a inner class as PInfo.
We call a method of PackageManager like
List<PackageInfo> packs = pm.getInstalledPackages(0);
As shown below.
/**** @param context* @param getSysPackages* @return List of installed application from the device*/private ArrayList<PInfo> getApps(Context context, boolean getSysPackages) {
final PackageManager pm = context.getPackageManager();ArrayList<PInfo> res = new ArrayList<PInfo>();List<PackageInfo> packs = pm.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {PackageInfo p = packs.get(i);if ((!getSysPackages) && (p.versionName == null)) {continue;}ApplicationInfo appinfo = p.applicationInfo;if(((appinfo.flags & ApplicationInfo.FLAG_SYSTEM) != (getSysPackages ? 1: ))))continue;if (pm.getLaunchIntentForPackage(p.packageName) == null) {continue;}PInfo info = new PInfo();info.appname= p.applicationInfo.loadLabel(pm).toString();info.pname = p.packageName;info.icon = p.applicationInfo.loadIcon(pm);res.add(newInfo);}Collections.sort(res, new AlPhabeticalComparator());return res;}
Now we have a list, we will just sort our array in alphabetical order using Java Collection API.
For that we require our custom comparator.
Create a Inner static class AlPhabeticalComarator
public static class AlPhabeticalComparator implements Comparator<PInfo> {@Overridepublic int compare(PInfo info1, PInfo info2) {return info1.appname.toUpperCase().compareTo(info2.appname.toUpperCase());}}
To store information of each application we require a Class Object Create as follows
Create a inner class as PInfo.
/**
* Static Inner class to hold application info
**/
public static class PInfo { public String appname = "";
public String pname = "";
public Drawable icon;
}