当前位置:   article > 正文

android像launcher一样获取手机应用列表_android context.getsystemservice(context.launcher_

android context.getsystemservice(context.launcher_apps_service)

下面代码是获取系统所有应用,下面判断条件是判断系统应用方法,如果是获取手机所有应用可以去掉下面判断条件

if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
                    (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
 }

  1. public static ArrayList getSystemApp(Context context, HashMap<String,SystemItem> itemDrawableMap){
  2. PackageManager mPackageManager = context.getApplicationContext().getPackageManager();
  3. LauncherApps mLauncherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
  4. UserManager mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
  5. List<UserHandle> users = mUserManager.getUserProfiles();
  6. List<UserHandle> profiles= users == null ? Collections.<UserHandle>emptyList() : users;
  7. ArrayList<Item> list = new ArrayList<>();
  8. //根据手机所有用户获取每个用户下的应用
  9. for (UserHandle user : profiles) {
  10. // Query for the set of apps
  11. final List<LauncherActivityInfo> apps = mLauncherApps.getActivityList(null, user);
  12. // Fail if we don't have any apps
  13. // TODO: Fix this. Only fail for the current user.
  14. if (apps == null || apps.isEmpty()) {
  15. continue;
  16. }
  17. // Create the ApplicationInfos
  18. for (int i = 0; i < apps.size(); i++) {
  19. LauncherActivityInfo app = apps.get(i);
  20. // This builds the icon bitmaps.
  21. ComponentName componentName = app.getComponentName();
  22. String appName =getSystemApplicationName(componentName.getPackageName(),mPackageManager);
  23. if(!TextUtils.isEmpty(appName)){
  24. Item model = new Item();
  25. model.setName(appName);
  26. SystemItem systemItem = new SystemItem();
  27. systemItem.setItemName(appName);
  28. systemItem.setPackName(componentName.getPackageName());
  29. systemItem.setIcon(app.getIcon(0));
  30. itemDrawableMap.put(model.itemName,systemItem);
  31. list.add(model);
  32. }
  33. }
  34. }
  35. return list;
  36. }
  37. public static String getSystemApplicationName(String packageName, PackageManager packageManager) {
  38. String applicationName = null;
  39. try {
  40. ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);
  41. //filter system app
  42. if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ||
  43. (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
  44. applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
  45. }
  46. } catch (PackageManager.NameNotFoundException e) {
  47. }
  48. return applicationName;
  49. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/377100?site
推荐阅读
相关标签
  

闽ICP备14008679号