Android获取应用信息

AndroidPackageManager大约 3154 字
public class AppInfoUtil {
    private PackageManager mPackageManager;
    private Context mContext;

    public static final int FILTER_ALL_APP = 0; // 所有应用程序
    public static final int FILTER_SYSTEM_APP = 1; // 系统程序
    public static final int FILTER_THIRD_APP = 2; // 第三方应用程序
    public static final int FILTER_SDCARD_APP = 3; // 安装在SDCard的应用程序

    private volatile static AppInfoUtil singleton = null;

    public static AppInfoUtil getInstance(Context context) {
        if (singleton == null) {
            synchronized (FlowUtil.class) {
                if (singleton == null) {
                    singleton = new AppInfoUtil(context);
                }
            }
        }
        return singleton;
    }

    private AppInfoUtil(Context context) {
        mContext = context.getApplicationContext();
        mPackageManager = mContext.getPackageManager();
    }

    @SuppressWarnings("deprecation")
    public List<AppInfo> queryAppInfo(int filter) {
        List<ApplicationInfo> applicationInfos = mPackageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        Collections.sort(applicationInfos,new ApplicationInfo.DisplayNameComparator(mPackageManager));// 排序
        List<AppInfo> appInfos = new ArrayList<>(); // 保存过滤查到的AppInfo
        // 根据条件来过滤
        switch (filter) {
            case FILTER_ALL_APP: // 所有应用程序
                appInfos.clear();
                for (ApplicationInfo app : applicationInfos) {
                    appInfos.add(getAppInfo(app));
                }
                return appInfos;
            case FILTER_SYSTEM_APP: // 系统程序
                appInfos.clear();
                for (ApplicationInfo app : applicationInfos) {
                    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                        appInfos.add(getAppInfo(app));
                    }
                }
                return appInfos;
            case FILTER_THIRD_APP: // 第三方应用程序
                appInfos.clear();
                for (ApplicationInfo app : applicationInfos) {
                    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {

                        appInfos.add(getAppInfo(app));
                    }
                }
                break;
            case FILTER_SDCARD_APP: // 安装在SDCard的应用程序
                appInfos.clear();
                for (ApplicationInfo app : applicationInfos) {
                    if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
                        appInfos.add(getAppInfo(app));
                    }
                }
                return appInfos;
            default:
                return null;
        }
        return appInfos;

    }

    // 构造一个AppInfo对象 ,并赋值
    private AppInfo getAppInfo(ApplicationInfo app) {
        AppInfo appInfo = new AppInfo();
        appInfo.appLabel = (String) app.loadLabel(mPackageManager);
        appInfo.appIcon = app.loadIcon(mPackageManager);
        appInfo.packageName = (app.packageName);
        appInfo.intent = (mPackageManager.getLaunchIntentForPackage(app.packageName));
        return appInfo;
    }

}
阅读 437 · 发布于 2019-04-13

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批