下面代码是获取系统所有应用,下面判断条件是判断系统应用方法,如果是获取手机所有应用可以去掉下面判断条件
if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
}
public static ArrayList getSystemApp(Context context, HashMap<String,SystemItem> itemDrawableMap){
PackageManager mPackageManager = context.getApplicationContext().getPackageManager();
LauncherApps mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
UserManager mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
List<UserHandle> users = mUserManager.getUserProfiles();
List<UserHandle> profiles= users == null ? Collections.<UserHandle>emptyList() : users;
ArrayList<Item> list = new ArrayList<>();
//根据手机所有用户获取每个用户下的应用
for (UserHandle user : profiles) {
// Query for the set of apps
final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
// Fail if we don't have any apps
// TODO: Fix this. Only fail for the current user.
if (apps == null || apps.isEmpty()) {
continue;
}
// Create the ApplicationInfos
for (int i = 0; i < apps.size(); i++) {
LauncherActivityInfo app = apps.get(i);
// This builds the icon bitmaps.
ComponentName componentName = app.getComponentName();
String appName =getSystemApplicationName(componentName.getPackageName(),mPackageManager);
if(!TextUtils.isEmpty(appName)){
Item model = new Item();
model.setName(appName);
SystemItem systemItem = new SystemItem();
systemItem.setItemName(appName);
systemItem.setPackName(componentName.getPackageName());
systemItem.setIcon(app.getIcon(0));
itemDrawableMap.put(model.itemName,systemItem);
list.add(model);
}
}
}
return list;
}
public static String getSystemApplicationName(String packageName, PackageManager packageManager) {
String applicationName = null;
try {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
//filter system app
if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
}
} catch (PackageManager.NameNotFoundException e) {
}
return applicationName;
}