我们经常会遇到这样的需求:列出手机中所有能打开的App。此时我们不需要列出那些无法打开的app,也就是没有Activity的。网上有一些方法,都是过滤系统app的方法,将短信、电话这些App都过滤掉了,其实只要使用下面代码就行了:
private void queryFilterAppInfo() {
PackageManager pm = this.getPackageManager();
// 查询所有已经安装的应用程序
List<ApplicationInfo> appInfos= pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
List<ApplicationInfo> applicationInfos=new ArrayList<>();
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
List<ResolveInfo> resolveinfoList = getPackageManager()
.queryIntentActivities(resolveIntent, 0);
Set<String> allowPackages=new HashSet();
for (ResolveInfo resolveInfo:resolveinfoList){
allowPackages.add(resolveInfo.activityInfo.packageName);
}
for (ApplicationInfo app:appInfos) {
// if((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0)//通过flag排除系统应用,会将电话、短信也排除掉
// {
// applicationInfos.add(app);
// }
// if(app.uid > 10000){//通过uid排除系统应用,在一些手机上效果不好
// applicationInfos.add(app);
// }
if (allowPackages.contains(app.packageName)){
applicationInfos.add(app);
}
}
mApplicationInfos=applicationInfos;
}