2

我有这段代码来加载图标和应用程序名称,但是超过 50 个应用程序速度很慢,加载需要 7-8 秒,如何更快地加载应用程序名称和图标?

private void loadApps() {
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            InternalDataManager.apps = getPackageManager()
                    .queryIntentActivities(mainIntent, 0);
            PackageManager pm = getPackageManager();

            for (int i = 0; i < InternalDataManager.apps.size(); i++) {

                ResolveInfo info = InternalDataManager.apps.get(i);

                            // PInfo holds name and icon
                PInfo infoP = new InternalDataManager.PInfo();

                infoP.appname = info.activityInfo.applicationInfo.loadLabel(pm)
                        .toString();


                infoP.icon = info.activityInfo.loadIcon(pm);

                infoP.pname = info.activityInfo.applicationInfo.packageName;



            }
        }
4

1 回答 1

1

这个帮助函数检索所有已安装的应用程序,包括应用程序名称、包名称、版本号和代码以及图标。getPackages() 方法返回一个包含所有应用程序的 ArrayList。你应该试试这个...

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-04-05T12:02:03.520 回答