列出Android中的应用程序图标

时间:2012-05-20 12:58:44

标签: android android-layout

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     lView = (ListView) findViewById(R.id.list1);
     PackageManager pm = this.getPackageManager();

     Intent intent = new Intent(Intent.ACTION_MAIN, null);
     intent.addCategory(Intent.CATEGORY_LAUNCHER);

     ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo : list) {

            results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm));


            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
        } 

     lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));

    }

如何修改此代码以便将这些列出的应用添加到自己的图标中?什么应该包含在main.xml中?

2 个答案:

答案 0 :(得分:1)

private ArrayList<PInfo> getInstalledApps() {
    ArrayList<PInfo> res = new ArrayList<PInfo>();        
    List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
    for(int i=0;i<packs.size();i++) {
        PackageInfo p = packs.get(i);
        PInfo pInfo = new PInfo();
        pInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
        pInfo.pname = p.packageName;
        pInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
        res.add(pInfo);
    }
    return res; 
}

以上返回PInfo的数组列表。这是所有已安装应用程序的appnams,图标和包名称列表。

答案 1 :(得分:1)

尝试使用此助手类

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