android像launcher一样获取手机应用列表

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

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;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值