Android How to - Get Non System APK file Path








Question

We would like to know how to get Non System APK file Path.

Answer

/*  w ww.  ja v a  2  s .  co  m*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {

  public static Map<String, PackageInfo> getNonSystemApkPathMap(Context context) {
    Map<String, PackageInfo> apkMap = new HashMap<String, PackageInfo>();
    List<PackageInfo> packageList = getAllPackages(context);
    if (packageList != null && packageList.size() > 0) {
      for (PackageInfo pkg : packageList) {
        if ((pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
          apkMap.put(pkg.packageName, pkg);
        }
      }
    }
    return apkMap;
  }

  public static List<PackageInfo> getAllPackages(Context context) {
    List<PackageInfo> packageList = new ArrayList<PackageInfo>();
    PackageManager pm = context.getPackageManager();
    packageList = pm.getInstalledPackages(0);
    return packageList;
  }
}