伊布SKY

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

      网络上有很多关于Android取所有应用列表的方法,但是取出来的都包含有非常多的应用,比系统显示出来的所有应用还要多。。查了一些方法后,发现以下面的代码即可取出和系统一模一样的应用列表了。

 

List<ApplicationEntity> applicationEntitys = new ArrayList<ApplicationEntity>();
  List<ApplicationEntity> applicationEntitysDB = getApplicationEntitys(true);
  PackageManager pManager = context.getPackageManager();
  final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  // 获取手机内所有应用
        final List<ResolveInfo> paklist = pManager.queryIntentActivities(mainIntent, 0);
  Log.d(TAG, " application.size()=" + pManager.getInstalledApplications(0).size());
  Log.d(TAG, " paklist.size()=" + paklist.size());
  for (int i = 0; i < paklist.size(); i++) {
   ResolveInfo pak = (ResolveInfo) paklist.get(i);
  
   boolean isSystem = !((pak.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0);
   ApplicationEntity applicationEntity = new ApplicationEntity();
   applicationEntity.setName(pak.loadLabel(pManager).toString());
   applicationEntity.setPackageName(pak.activityInfo.applicationInfo.packageName);
   applicationEntity.setProcessName(pak.activityInfo.applicationInfo.processName);
   applicationEntity.setIconBlob(StreamCtrls.drawableToByte(pManager
     .getApplicationIcon(pak.activityInfo.applicationInfo)));
   applicationEntity.setSystem(isSystem);
   boolean isSelected = false;
   for (int j = 0; j < applicationEntitysDB.size(); j++) {
    ApplicationEntity AppDB = applicationEntitysDB.get(j);
    if (AppDB.getPackageName().equals(
      applicationEntity.getPackageName())) {
     applicationEntity.setId(AppDB.getId());
     isSelected = AppDB.isSelect();
     break;
    }
   }
   applicationEntity.setSelect(isSelected);
   applicationEntitys.add(applicationEntity);
  }

  for (int i = 0; i < applicationEntitys.size(); i++) {
   ApplicationEntity applicationEntity = applicationEntitys.get(i);
   ApplicationAdapter operateAdapter;
   if (applicationEntity.isSystem())
    operateAdapter = sysAdapter;
   else
    operateAdapter = cusAdapter;
   operateAdapter.AddItem(applicationEntity);
  }

 

posted on 2012-03-21 15:09  伊布  阅读(202)  评论(0编辑  收藏  举报