Android Context Get getApp(Context ctx, String packageName)

Here you can find the source of getApp(Context ctx, String packageName)

Description

get App

Declaration

public static String getApp(Context ctx, String packageName) 

Method Source Code

//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;
    }
}

Related

  1. cachedJson(Context context, URI uri)
  2. cacheJson(Context context, URI uri, String jsonSource)
  3. getSharedPreferences(Context context)
  4. getAndroidDBDir(Context context)
  5. getAnimId(Context paramContext, String paramString)
  6. getAppKey(Context context)
  7. getAppVersion(Context context)
  8. getApplicationInfos(Context context)
  9. getApplicationMemoryClass(@Nonnull Context context)