我有这个代码来加载图标和应用程序名称,但是对于超过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;
}
}
答案 0 :(得分: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;
}