注意:在Android11版本的时候不给获取了,相关文章如下:
效果图
获取手机上已安装应用列表,将获取到的信息用集合返回,可以自己定义是否过滤系统应用,Appinfo是一个实体类,包含应用的名称 包名 图标icon等等
/**
* 获取手机已安装应用列表
* @param ctx
* @param isFilterSystem 是否过滤系统应用
* @return
*/
public static ArrayList<AppInfo> getAllAppInfo(Context ctx,boolean isFilterSystem) {
ArrayList<AppInfo> appBeanList = new ArrayList<>();
AppInfo bean = null;
PackageManager packageManager = ctx.getPackageManager();
List<PackageInfo> list = packageManager.getInstalledPackages(0);
for (PackageInfo p : list) {
bean = new AppInfo();
bean.setIcon(p.applicationInfo.loadIcon(packageManager));
bean.setLabel(packageManager.getApplicationLabel(p.applicationInfo).toString());
bean.setPackage_name(p.applicationInfo.packageName);
int flags = p.applicationInfo.flags;
// 判断是否是属于系统的apk
if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0&&isFilterSystem) {
// bean.setSystem(true);
} else {
appBeanList.add(bean);
}
}
return appBeanList;
}
Appinfo
public class AppInfo {
public int uid;
public String label;//应用名称
public String package_name;//应用包名
public Drawable icon;//应用icon
public AppInfo() {
uid = 0;
label = "";
package_name = "";
icon = null;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPackage_name() {
return package_name;
}
public void setPackage_name(String package_name) {
this.package_name = package_name;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}