0
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 中应该包含什么?

4

2 回答 2

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 的数组列表。那是所有已安装应用程序的应用程序名称、图标和包名称的列表。

于 2012-05-20T13:03:47.850 回答
1

尝试使用这个 Helper 类

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; 
}
于 2012-05-20T13:07:30.310 回答