2012-04-05 18 views
1

我有这个代码来加载图标和应用程序名称,但它对于超过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

该代码应该需要几毫秒的时间才能运行。你怎么确定这是你的问题的代码块? Traceview指出,对于那些速度缓慢的具体电话,这是什么意思? – CommonsWare 2012-04-05 11:55:14

+0

加载图标需要时间,真的时间 – Ata 2012-04-05 12:02:07

+0

'loadIcon()'应该花费很少的时间。这个示例项目可以在一眨眼之间提出一个充满了可启动项目的ListView:https://github.com/commonsguy/cw-advandroid/tree/master/Introspection/Launchalot – CommonsWare 2012-04-05 12:29:25

回答

1

这个辅助函数检索所有与应用程序的名称,包装名称,版本号和-code以及图标安装的应用程序。方法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; 
} 
+0

它列出了所有的服务和其他活动,我需要启动器活动 – Ata 2012-04-05 12:11:42

+0

我同意第一代码需要24秒。限制代码需要18秒。但仍然没有使用packageManager是200毫秒。 – Tefel 2012-08-23 20:53:59

相关问题