在开发中 ,有时候会遇到获取当前安装应用的列表这种需求,现在就来讲解下实现它的两种方法
1.通过PackageInfo获取:
public static List<PackageInfo> getAllApps(Context context) {
List<PackageInfo> apps = new ArrayList<PackageInfo>();
PackageManager pManager = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> packlist = pManager.getInstalledPackages(0);
for (int i = 0; i < packlist.size(); i++) {
PackageInfo pak = (PackageInfo) packlist.get(i);
// if()里的值如果<=0则为自己装的程序,否则为系统工程自带
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
// 添加自己已经安装的应用程序
// apps.add(pak);
} else{ // 系统应用
}
apps.add(pak);
}
return apps;
}
2.通过使用ResolveInfo获取:
public void loadAllAppsByBatch() {
PackageManager packageManager = mContext.getPackageManager();
List apps = null;
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
apps = packageManager.queryIntentActivities(mainIntent, 0);
for (int i = 0; i < apps.size(); i++) {
String packageName = apps.get(i).activityInfo.applicationInfo.packageName;
String title = apps.get(i).loadLabel(packageManager).toString();
Drawable icon = null;
if(title == null){
title = apps.get(i).activityInfo.name;
}
ActivityInfo info = apps.get(i).activityInfo;
icon = getFullResIcon(info);
datas.add(new PakageMod(packageName,title,icon));
}
}