This helper function retrieves all installed apps with the application name, package name, version-number and -code as well as the icons. The method getPackages()
returns an ArrayList with all the apps.
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
12 Comments
Nice post,
Thanks for bringing this up
thansk for the help! its working perfectly!
i cant get it to work at all, what must you do to get it to work?
It depends what you want to do, but you can use
getPackages()
to return you an ArrayList of all the installed items.if you are getting error in Log.v(appname + "t" + pname + "t" + versionName + "t" + versionCode); then replace this line with System.out.println(appname + "t" + pname + "t" + versionName) + "t" + versionCode + "t");
I'm very new to Android development and I'm not really sure how to use this snippet. Would someone please provide me with an example? (Maybe a blank gui using this?)
Thanks!
Hi, been working on your snippets for quite some time now and unfortunately, it does show system apps, no matter what I do.
Tried modifying the continue statement to: if ((packs.get(i).versionCode & ApplicationInfo.FLAG_SYSTEM) == 1 && !getSysPackages) { continue; } it does screen most system apps but not all of them and also removes some user installed apps, so it's no good...
Would love if you can update you code or tell me what i'm missing here :)
Never mind, here is the right if statement: if ((packs.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { continue; }
Thank you for the rest of the great code!
Can you mention the packages that you imported ?
Can you mention the packages that you imported ?
very very good thank you for giving such a nice code fabulous....it's save my time
Hi using p.versionname= null is pulling few apps installed by google how to avoid it and just get list of user installed apps