获取安卓后台正在运行的应用程序(不包括当前应用)

时间:2021-11-03 05:02:26
/** * 获取正在运行的APP列表-------------放到子线程中执行 */
    private List<AppInfo> getAppInfos() {
        List<AppInfo> appInfos = new ArrayList<>();

        // 获取正在运行的进程
        ActivityManager activityManager = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();

        // 查询所有已经安装的应用程序
        PackageManager packageManager = mActivity.getPackageManager();
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);

        for (RunningAppProcessInfo appProcess : appProcesses) {
            String[] pkgList = appProcess.pkgList;
            for (String packageName : pkgList) {
                for (PackageInfo packageInfo : packageInfos) {
                    if (TextUtils.equals(packageInfo.packageName, packageName)) {
                        if (TextUtils.equals(packageName, mActivity.getPackageName())) {
                            // 如果是本应用程序,则不要添加。
                            break;
                        }
                        // 该已安装的应用程序和正在后台运行的这个应用程序是同一个
                        AppInfo appInfo = new AppInfo();
                        // 获得应用名
                        appInfo.setName(packageManager.getApplicationLabel(packageInfo.applicationInfo).toString());
                        // 获得应用包名
                        appInfo.setPackageName(packageName);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                        appInfo.setIcon(packageInfo.applicationInfo.loadIcon(packageManager));
                        appInfo.setPid(appProcess.pid);
                        appInfos.add(appInfo);
                        break;
                    }
                }
            }
        }

        // 排序
        Collections.sort(appInfos, new Comparator<AppInfo>() {
            @Override
            public int compare(AppInfo lhs, AppInfo rhs) {
                // TODO Auto-generated method stub
                return lhs.getName().compareTo(rhs.getName());
            }
        });

        return appInfos;
    }