Here you can find the source of getApp(Context ctx, String packageName)
public static String getApp(Context ctx, String packageName)
//package com.java2s; import java.io.File; import java.util.ArrayList; import java.util.List; import org.json.JSONObject; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.text.format.Time; public class Main { public static String getApp(Context ctx, String packageName) { JSONObject json = new JSONObject(); try {//from ww w .j a v a 2s . c om PackageManager pm = ctx.getPackageManager(); Intent it = new Intent("android.intent.action.MAIN"); //$NON-NLS-1$ it.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> acts = pm.queryIntentActivities(it, 0); int actsSize = acts.size(); List<ApplicationInfo> allApps = pm.getInstalledApplications(0); List<ApplicationInfo> filteredApps = filterApps(allApps); Time trackTime = new Time(); for (int i = 0, size = filteredApps.size(); i < size; i++) { ApplicationInfo info = filteredApps.get(i); try { PackageInfo pi = pm.getPackageInfo(info.packageName, 0); String pn = pi.packageName; if (pn.equalsIgnoreCase(packageName)) { json.put("pn", pn); json.put("cv", String.valueOf(pi.versionCode)); if (pi.applicationInfo.sourceDir != null) { File f = new File(pi.applicationInfo.sourceDir); trackTime.set(f.lastModified()); json.put("it", trackTime.format("%Y%m%d%H%M%S")); } else { trackTime.setToNow(); json.put("it", trackTime.format("%Y%m%d%H%M%S")); } json.put("cn", pi.applicationInfo.loadLabel(pm) .toString()); for (int j = 0; j < actsSize; j++) { ResolveInfo ri = acts.get(j); if (pi.packageName .equals(ri.activityInfo.packageName)) { json.put("an", ri.activityInfo.name); break; } } return json.toString(); } } catch (NameNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return json.toString(); } static List<ApplicationInfo> filterApps(List<ApplicationInfo> apps) { if (apps == null || apps.size() == 0) { return apps; } List<ApplicationInfo> userApps = new ArrayList<ApplicationInfo>(); for (int i = 0, size = apps.size(); i < size; i++) { ApplicationInfo ai = apps.get(i); if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { userApps.add(ai); } } return userApps; } }