public List<AppInfo> getAllApps( final OnInstalledPackagesCallback callback) {
LogUtils.d(TAG,"getAllApps() getInstalledPackages start=" + (System.currentTimeMillis()));
if (callback!=null)
callback.onStartGet();
AppInfo myAppInfo;
/**
* GET_ACTIVITIES, GET_CONFIGURATIONS, GET_GIDS, GET_INSTRUMENTATION, GET_INTENT_FILTERS,
* GET_META_DATA, GET_PERMISSIONS, GET_PROVIDERS, GET_RECEIVERS, GET_SERVICES,
* GET_SHARED_LIBRARY_FILES, GET_SIGNATURES, GET_URI_PERMISSION_PATTERNS,
* MATCH_DISABLED_COMPONENTS, MATCH_DISABLED_UNTIL_USED_COMPONENTS,
* MATCH_UNINSTALLED_PACKAGES to modify the data returned.
*/
//獲取到所有安裝了的應用程序的信息,包括那些卸載了的校仑,但沒有清除數(shù)據(jù)的應用程序
List<PackageInfo> packageInfos = packageManager
.getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES);
mStringBuilder = new StringBuilder("\"");
if (null != mAppInfoAlls) {
mAppInfoAlls.clear();
} else {
mAppInfoAlls = new ArrayList<>();
}
for (PackageInfo info : packageInfos) {
if ( info.activities == null || info.activities.length == 0) {
//沒有activity ,表示不能打開
continue;
}
Intent queryActIntent = new Intent(Intent.ACTION_MAIN,null);
queryActIntent.addCategory(Intent.CATEGORY_LAUNCHER);
queryActIntent.setPackage(info.packageName);
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(queryActIntent,0);
if (resolveInfoList == null || resolveInfoList.isEmpty()) {
continue;
}
ResolveInfo resolveinfo = resolveInfoList.iterator().next();
if (resolveinfo == null) {
continue;
}
String actClsName = resolveinfo.activityInfo.name;
if (actClsName == null) {
continue;
}
myAppInfo = new AppInfo();
//拿到包名
String packageName = info.packageName;
//拿到應用程序的信息
ApplicationInfo appInfo = info.applicationInfo;
//拿到應用程序的大小
//long codesize = packageStats.codeSize;
myAppInfo.setPackageName(packageName);
//拿到應用程序的程序名
String appName = packageManager.getApplicationLabel(appInfo).toString();
myAppInfo.setAppName(appName);
//拿到應用程序的圖標
myAppInfo.setIcon(packageManager.getApplicationIcon(appInfo));
ConstUtil.saveWechatVersion(info);
if (filterApp(appInfo)) {
myAppInfo.setSystemApp(false);
} else {
myAppInfo.setSystemApp(true);
}
mStringBuilder.append(myAppInfo.appName + "\",\"");
mAppInfoAlls.add(myAppInfo);
LogUtils.d(TAG,"getAllApps() info.name =" + (packageManager.getApplicationLabel(info.applicationInfo).toString()));
}
if (callback!=null)
callback.onFinish(mAppInfoAlls);
LogUtils.d(TAG,"getAllApps() getInstalledPackages end=" + (System.currentTimeMillis()));
return mAppInfoAlls;
}
//這個方法很多app 不會顯示出來
public void getAllAppsCanOpen() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通過查詢肤视,獲得所有ResolveInfo對象.
List<ResolveInfo> resolveInfos = packageManager
.queryIntentActivities(mainIntent,0);
// 調(diào)用系統(tǒng)排序 涉枫, 根據(jù)name排序
// 該排序很重要,否則只能顯示系統(tǒng)應用困后,而不能列出第三方應用程序
Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(packageManager));
mStringBuilder = new StringBuilder("");
if (mAppInfoAlls != null) {
mAppInfoAlls.clear();
} else {
mAppInfoAlls = new ArrayList<>();
}
for (ResolveInfo reInfo : resolveInfos) {
String activityName = reInfo.activityInfo.name; // 獲得該應用程序的啟動Activity的name
String pkgName = reInfo.activityInfo.packageName; // 獲得應用程序的包名
String appLabel = (String)reInfo.loadLabel(packageManager); // 獲得應用程序的Label
Drawable icon = reInfo.loadIcon(packageManager); // 獲得應用程序圖標
// 為應用程序的啟動Activity 準備Intent
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName(pkgName,
activityName));
// 創(chuàng)建一個AppInfo對象衬廷,并賦值
AppInfo appInfo = new AppInfo();
appInfo.setAppName(appLabel);
appInfo.setPackageName(pkgName);
appInfo.setIcon(icon);
appInfo.setIntent(launchIntent);
mStringBuilder.append(appLabel + "\n");
mAppInfoAlls.add(appInfo); // 添加至列表中
}
}
//這里是首字母升序排列
Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(packageManager));
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者