/**
* 获取所有应用
* @return 所有应用的集合
*/
private List<AppInfo> queryAppInfo() {
mlistAppInfo = new ArrayList<AppInfo>();
PackageManager pm = this.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0);
//调用系统排序 , 根据name排序
Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));
if (mlistAppInfo != null) {
mlistAppInfo.clear();
for(ResolveInfo resolveInfo : resolveInfos) {
String activityName = resolveInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
String pkgName = resolveInfo.activityInfo.packageName; // 获得应用程序的包名
String appLabel = (String)resolveInfo.loadLabel(pm); //获取应用的名称
Drawable icon = resolveInfo.loadIcon(pm); //获取应用的图标icon
Log.i("ii", "========="+activityName+" ====== "+pkgName);
//为应用程序的启动Activity 准备Intent
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName(pkgName,activityName));
// 创建一个AppInfo对象,并赋值
AppInfo appInfo = new AppInfo();
appInfo.setAppLabel(appLabel);
appInfo.setPkgName(pkgName);
appInfo.setAppIcon(icon);
appInfo.setIntent(launchIntent);
mlistAppInfo.add(appInfo); // 添加至列表中
}
}
return mlistAppInfo;
}
获取系统中所有应用的方法
最新推荐文章于 2022-01-07 11:30:02 发布