获取手机应用排除系统无法打开的组件,按照安装顺序排序


获取手机应用排除系统无法打开的组件,按照安装顺序排序


private void initData() {

if (mSwitchBean !=null)

mTv_showMsg.setText(getActivity().getString(mSwitchBean.getUserId()) +"---" +mSwitchBean.getTitleId() +"---" +mSwitchBean.getDescription() +"---" +mSwitchBean.getUrl());

        PackageManager pm = getActivity().getPackageManager();

// 查询所有已经安装的应用程序

        List appInfos= pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent

        Intent resolveIntent =new Intent(Intent.ACTION_MAIN, null);

        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        // 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName

        List  resolveinfoList = getActivity().getPackageManager()

        .queryIntentActivities(resolveIntent, 0);

        Set allowPackages=new HashSet();

        for (ResolveInfo resolveInfo:resolveinfoList){

allowPackages.add(resolveInfo.activityInfo.packageName);

        }

for (ApplicationInfo app:appInfos) {

            if (allowPackages.contains(app.packageName)){

                        long installtime =0;

                try {

                        installtime = pm.getPackageInfo(app.packageName, 0).lastUpdateTime;

                }catch (PackageManager.NameNotFoundException e) {

                        e.printStackTrace();

                }

        AppsBean appsBean =new AppsBean(app.packageName, app.loadIcon(pm), app.loadLabel(pm).toString(), installtime);

                Log.i("TAGt", "initData: "+installtime);

                mAppList.add(appsBean);

                sortData((ArrayList)mAppList);

            }

}

        mAdapter.notifyDataSetChanged();

    }

@Override

    public void setSwitchBean(SwitchBean switchBean) {

        mSwitchBean = switchBean;

        super.setSwitchBean(switchBean);

    }

private void sortData(ArrayList mList) {

Collections.sort(mList, new Comparator() {

/**

*

            * @param lhs

            * @param rhs

            * @return an integer < 0 if lhs is less than rhs, 0 if they are

            *        equal, and > 0 if lhs is greater than rhs,比较数据大小时,这里比的是时间

            */

            @Override

            public int compare(AppsBean lhs, AppsBean rhs) {

                long date1 = lhs.getInstallTime();

                long date2 = rhs.getInstallTime();

                // 对时间字段进行降序

                if (date1 > date2) {

                        return 1;

                }

                        return -1;

            }

    });

 }

你可能感兴趣的:(获取手机应用排除系统无法打开的组件,按照安装顺序排序)