2014-10-08 67 views
1

第一次尝试:的Android - 安装的应用程序列表获取时间过长

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);    
    Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); 
    pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); 
    this.startActivityForResult(pickIntent, MoreIconsConstants.REQUEST_PICK_APPLICATION) 

所需的平均时间 - > 8-10第二

第二次尝试我试图让程序列表中的应用程序列表并在对话框中显示我的自我列表.----->约4秒...更快但仍然很慢。

第三次尝试:我将列表存储在我的首选项文件中,以便将来我立即加载该列表...同时获取当前列表的背景,如果存在差异更新列表显示给用户 - ---也约4秒。

这是奇怪的地方。使用日志报表我测量了每种方法所需的确切时间。 如果我首先加载从prefferences列表,然后如果i装入列表首先查询包管理器通过查询我再次需要4秒钟的偏好方法和用于所述查询方法

0.5秒包管理器 加载它然后从首选项中加载它,查询方法需要大约4秒,而首选方法需要0.5秒

因此,无论我做什么,第一种方法都会花费大量时间,第二种方法会立即执行。

有没有解释为此或其他方式加载此列表更快?

我引用我的代码,这两种方法

货清单从喜好方法quering包管理器

private class AppAsyncCaller extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { super.onPreExecute(); } 
    @Override 
    protected Void doInBackground(Void... params) { 
     ArrayList<AppItem> allAppsInDevice2 = new ArrayList<AppItem>(); 
     long timeStart=System.currentTimeMillis(); 
     Intent mainIntent = new Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER); 
     List<ResolveInfo> packages=pm.queryIntentActivities(mainIntent, 0); 
     for(int i=0;i<packages.size();i++){ 
      try{ 
       String packageName=packages.get(i).activityInfo.packageName; 
       AppItem item=getAppItem(packageName,false); 
       if(item!=null){allAppsInDevice2.add(item);} 
      }catch(Exception e){} 
     } 
     Log.w(Long.toString(System.currentTimeMillis()-timeStart),"duration using async caller"); 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { super.onPostExecute(result); } 

} 

货清单:

private class AppPrefsAsyncCaller extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { super.onPreExecute(); } 
    @Override 
    protected Void doInBackground(Void... params) { 
     long timeStart=System.currentTimeMillis(); 
     String allAppsListString = prefs.getString("allAppsListString", ""); 
     String[] tab=allAppsListString.split("_APPAPPAPP_"); 
     allAppsInDevice.clear(); 
     boolean updateAllApps=false; 
     for(String s:tab){ 
      if(!s.equals("") && !s.equals(" ")){ 
       AppItem item=getAppItem(s,false); 
       if(item!=null){ allAppsInDevice.add(item); } 
      } 
     } 

     Log.w(Long.toString(System.currentTimeMillis()-timeStart),"duration apo pref"); 


     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { super.onPostExecute(result); } 

} 




public AppItem getAppItem(String packageName,boolean getIcon){ 
     AppItem item=new AppItem(); 
     item.packageName=packageName; 
     ApplicationInfo ai=null; 
     try { ai = pm.getApplicationInfo(packageName, 0); } 
     catch (final NameNotFoundException e) {return null; } 
     final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 
     item.appName=applicationName; 
     Intent intent = pm.getLaunchIntentForPackage(packageName); 
     if(getIcon){ 
     Drawable icon=null; 
      if (intent != null) { try { icon = pm.getActivityIcon(intent); } catch (NameNotFoundException e) {} } 
      item.icon=icon; 
     } 

     return item; 
    } 
public class AppItem{ 
    String packageName; 
    String appName; 
    Drawable icon; 
} 
+0

使用Traceview并确定问题的确切位置。 – CommonsWare 2014-10-08 20:34:26

回答

0

您可以与Android PackageManager做到这一点。

下面是一个小的代码片段。

final PackageManager pm = getPackageManager(); 
//get a list of installed apps. 
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); 

for (ApplicationInfo packageInfo : packages) { 
    Log.d(TAG, "Installed package :" + packageInfo.packageName); 
    Log.d(TAG, "Apk file path:" + packageInfo.sourceDir); 
} 
相关问题