Try popular Android Application by Feliz-Droid

Try popular Android Application by Feliz-Droid

Speaking Caller logo
Speaking Caller
   
Battery temperature logo
Battery Temperature
   
Dual Photo logo
Dual Photo
   
Ganesh Wallpapersl ogo
Lord Ganesh Wallpaper
   
Girly Pink Keyboard l ogo
Girly Pink Keyboard

Thursday, May 2, 2013

How to get List of all installed application from device

Hello Friends, We will see  how to  get list of all installed application from android device programmatically,

We call a method of PackageManager like

List<PackageInfo> packs = pm.getInstalledPackages(0);

It returns a list of packageInfo object. Now we will strore our app list in ArrayList.

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> {
     @Override
     public 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;

3 comments:

  1. Thanks dude.. aweome, I was searching for :

    public static class AlPhabeticalComparator implements Comparator {
    @Override
    public int compare(PInfo info1, PInfo info2) {
    return info1.appname.toUpperCase().compareTo(info2.appname.toUpperCase());
    }
    }

    and the search ends here. :))

    ReplyDelete
  2. Thanks I get help using your code

    ReplyDelete
  3. Can you help me how we get app installed date & app size?

    ReplyDelete