-1

I am new in android.I had to retrieve list of all installed application package names using the following code

    List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
    for(PackageInfo pack : packages)
    {
        ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities;
        Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities");
        if(activityInfo!=null)
        {
            for(int i=0; i<activityInfo.length; i++)
            {
                Log.i("PC",""+ activityInfo[i]);
                myList = new ArrayList();
                myList.add(pack.packageName);

            }
            aplist = new ArrayList<String>();
            aplist.add(pack.packageName);
            Toast.makeText(this, "list are      "+aplist, Toast.LENGTH_LONG).show();

        }


    }

but it gets the packagename in diff toast. How Can I get all package name in a single list ??

Maxwell
  • 552
  • 2
  • 5
  • 20

2 Answers2

1

Below helper function retrieves all installed apps with the application name, package name, version-number and -code as well as the icons. The method getPackages() returns an ArrayList with all the apps.

class PkgInfo {
    private String appname = "";
    private String pname = "";
    private String versionName = "";
    private int versionCode = 0;
    private Drawable icon;
    private void prettyPrint() {
        Log.v(appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
    }
}

private ArrayList<PkgInfo> getPackages() {
    ArrayList<PkgInfo> apps = getInstalledApps(false); /* false = no system packages */
    final int max = apps.size();
    for (int i=0; i<max; i++) {
        apps.get(i).prettyPrint();
    }
    return apps;
}

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

Hope this helps you.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Why are you initializing apList and myList variable in each iteration?. Have a look at the modified code below which should solve your problem:

aplist = new ArrayList<String>();
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(PackageInfo pack : packages)
{
    ActivityInfo[] activityInfo = getPackageManager().getPackageInfo(pack.packageName, PackageManager.GET_ACTIVITIES).activities;
    Log.i("Pranay", pack.packageName + " has total " + ((activityInfo==null)?0:activityInfo.length) + " activities");
    if(activityInfo!=null)
    {
        for(int i=0; i<activityInfo.length; i++)
        {
            Log.i("PC",""+ activityInfo[i]);

            if (myList != null)
                myList = new ArrayList();

            myList.add(pack.packageName);
        }

        aplist.add(pack.packageName);
    }
}
Toast.makeText(this, "list are      "+aplist, Toast.LENGTH_LONG).show();