实体类 AppInfo
public class AppInfo {
public String name;
public String packageName;
public Drawable icon;
public boolean isRom; //true表示安装在手机内存
public boolean isUser; //true表示用户应用
}
获取已安装应用列表
public ArrayList<AppInfo> getInstallApps(Context context){
PackageManager pm = context.getPackageManager();
List<PackageInfo> installedPackages = pm.getInstalledPackages(0); //获取所以已安装的包
ArrayList<AppInfo> list = new ArrayList<AppInfo>();
for( PackageInfo packageInfo : installedPackages){
AppInfo info = new AppInfo();
String packageName = packageInfo.packageName; //包名
ApplicationInfo applicationInfo = packageInfo.applicationInfo; //应用信息
String name = applicationInfo.loadLabel(pm).toString(); //应用名称
Drawable icon = applicationInfo.loadIcon(pm); //应用图标
info.name = name;
info.packageName = packageName;
info.icon = icon;
//状态机,通过01状态来表示是否具备某些属性和功能
int flags = applicationInfo.flags; //获取应用标记
if((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo
.FLAG_EXTERNAL_STORAGE){
//安装在sdcard
info.isRom = false;
}else{
//安装在手机
info.isRom = true;
}
if((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo
.FLAG_SYSTEM){
//系统应用
info.isUser = false;
}else{
//用户应用
info.isUser = true;
}
list.add(info);
}
return list;
}