Monday, January 16, 2012

Get installed Applications with Details

AIM:  To retrieve all installed apps with the application name, package name, version-number and -code as well as the icons.
Classes:  method getPackages() returns an ArrayList with all the apps.
Code snippet:

class PackageInfoData{
    private String appname = "";
    private String pname = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;
}

private void getInstalledPackages() {
    ArrayList<PackageInfoData> installedApps = getInstalledApps(false); /* false = no system packages */
    for (int i=0; i<installedApps.size(); i++) {
       Log.i("Instlled app","APp Name:"+installedApps.get(i).appname+"package:"+installedApps.get(i).pname);
    }
    return installedApps ;
}

private ArrayList<PackageInfoData> getInstalledApps(boolean getSysPackages) {
    ArrayList<PackageInfoData> res = new ArrayList<PackageInfoData>();       
    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 ;
        }
        PackageInfoDatanewInfo = new PackageInfoData();
        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:

  1. Thanks for a marvelous posting! I quite enjoyed reading
    it, you may be a great author.I will remember to bookmark your blog and will come back in the future.

    I want to encourage you continue your great posts, have a nice
    day!

    Also visit my blog :: rozliczenie pit 2014 ()

    ReplyDelete

Android Developers Blog

Ram's shared items