如何在列表中获取已安装的软件包名称?

时间:2014-05-14 10:35:16

标签: android package

我是android的新手。我必须使用以下代码检索所有已安装的应用程序包名称的列表

    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();

        }


    }

但它获取diff toast中的packagename。如何将所有包名称放在一个列表中?

2 个答案:

答案 0 :(得分:0)

为什么要在每次迭代中初始化apList和myList变量? 看看下面的修改后的代码应该可以解决你的问题:

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();

答案 1 :(得分:0)

helper函数下面检索所有已安装的应用程序,包括应用程序名称,包名称,版本号和代码以及图标。方法getPackages()会返回包含所有应用的ArrayList

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

希望这会对你有所帮助。