public class AppInfoProvider {
/**
* 返回当前手机所有的应用的相关信息(名称,包名,图标,(手机内存,sd卡),(系统,用户));
* @param ctx 获取包管理者的上下文环境
* @return 包含手机安装应用相关信息的集合
*/
public static List<AppInfo> getAppInfoList(Context ctx){
//1,包的管理者对象
PackageManager pm = ctx.getPackageManager();
//2,获取安装在手机上应用相关信息的集合
List<PackageInfo> packageInfoList = pm.getInstalledPackages(0);
List<AppInfo> appInfoList = new ArrayList<AppInfo>();
//3,循环遍历应用信息的集合
for (PackageInfo packageInfo : packageInfoList) {
AppInfo appInfo = new AppInfo();
//4,获取应用的包名
appInfo.packageName = packageInfo.packageName;
//5,应用名称
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
//applicationInfo.uid获取每一个应用的唯一性标示
appInfo.name = applicationInfo.loadLabel(pm).toString()+applicationInfo.uid;
//6,获取图标
appInfo.icon = applicationInfo.loadIcon(pm);
//7,判断是否为系统应用(每一个手机上的应用对应的flag都不一致)
if((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)==ApplicationInfo.FLAG_SYSTEM){
//系统应用
appInfo.isSystem = true;
}else{
//非系统应用
appInfo.isSystem = false;
}
//8,是否为sd卡中安装应用
if((applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE)==ApplicationInfo.FLAG_EXTERNAL_STORAGE){
//系统应用
appInfo.isSdCard = true;
}else{
//非系统应用
appInfo.isSdCard = false;
}
appInfoList.add(appInfo);
}
return appInfoList;
}
}
public class AppInfo {
// 名称,包名,图标,(内存,sd卡),(系统,用户)
public String name;
public String packageName;
public Drawable icon;
public boolean isSdCard;
public boolean isSystem;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public boolean isSdCard() {
return isSdCard;
}
public void setSdCard(boolean isSdCard) {
this.isSdCard = isSdCard;
}
public boolean isSystem() {
return isSystem;
}
public void setSystem(boolean isSystem) {
this.isSystem = isSystem;
}
}