ApplicationInfo是從一個特定的應用獲得的信息。這些信息是從相對應的Androdimanifest.xml的< application>標籤中收集到的,可獲取應用程序啓動Activity的name。app
// 根據查詢條件,查詢特定的ApplicationInfo private List<AppInfo> queryFilterAppInfo(int filter) { pm = this.getPackageManager(); // 查詢全部已經安裝的應用程序 List<ApplicationInfo> listAppcations = pm .getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); // 該排序很重要,不然只能顯示系統應用,而不能列出第三方應用程序 Collections.sort(listAppcations, new ApplicationInfo.DisplayNameComparator(pm));// 排序 List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存過濾查到的AppInfo // 根據條件來過濾 switch (filter) { case FILTER_ALL_APP: // 全部應用程序 appInfos.clear(); for (ApplicationInfo app : listAppcations) { appInfos.add(getAppInfo(app)); } return appInfos; case FILTER_SYSTEM_APP: // 系統程序 appInfos.clear(); for (ApplicationInfo app : listAppcations) { if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { appInfos.add(getAppInfo(app)); } } return appInfos; case FILTER_THIRD_APP: // 第三方應用程序 appInfos.clear(); for (ApplicationInfo app : listAppcations) { //非系統程序 if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { appInfos.add(getAppInfo(app)); } //原本是系統程序,被用戶手動更新後,該系統程序也成爲第三方應用程序了 else if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) { appInfos.add(getAppInfo(app)); } } break; case FILTER_SDCARD_APP: // 安裝在SDCard的應用程序 appInfos.clear(); for (ApplicationInfo app : listAppcations) { if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) { appInfos.add(getAppInfo(app)); } } return appInfos; default: return null; } return appInfos; } // 構造一個AppInfo對象 ,並賦值 private AppInfo getAppInfo(ApplicationInfo app) { AppInfo appInfo = new AppInfo(); appInfo.setAppLabel((String) app.loadLabel(pm)); appInfo.setAppIcon(app.loadIcon(pm)); appInfo.setPkgName(app.packageName); return appInfo; }