Get all the application information in the phone in Android

Sometimes we need to obtain information about all the software installed in the mobile phone during development, such as judging whether a certain software is installed, or obtaining the specific package name of a certain software. Here I post related methods.

First of all, we need to have an APP entity class

public class AppInfo {
    public int uid;
    public String label;
    public String package_name;
    public Drawable icon;
    public long traffic;

    public long rowid;
    public int xuhao;
    public int month;
    public int day;
    public String icon_path;

    public AppInfo() {
        uid = 0;
        label = "";
        package_name = "";
        icon = null;
        traffic = 0;

        rowid = 0;
        xuhao = 0;
        month = 0;
        day = 0;
        icon_path = "";
    }

Secondly, we need a method

   // 获取已安装的应用信息队列
    public static ArrayList<AppInfo> getAppInfo(Context ctx, int type) {
        ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
        SparseIntArray siArray = new SparseIntArray();
        // 获得应用包管理器
        PackageManager pm = ctx.getPackageManager();
        // 获取系统中已经安装的应用列表
        @SuppressLint("WrongConstant")
        List<ApplicationInfo> installList = pm.getInstalledApplications(
                PackageManager.PERMISSION_GRANTED);
        for (int i = 0; i < installList.size(); i++) {
            ApplicationInfo item = installList.get(i);
            // 去掉重复的应用信息
            if (siArray.indexOfKey(item.uid) >= 0) {
                continue;
            }
            // 往siArray中添加一个应用编号,以便后续的去重校验
            siArray.put(item.uid, 1);
            try {
                // 获取该应用的权限列表
                String[] permissions = pm.getPackageInfo(item.packageName,
                        PackageManager.GET_PERMISSIONS).requestedPermissions;
                if (permissions == null) {
                    continue;
                }
                boolean isQueryNetwork = false;
                for (String permission : permissions) {
                    // 过滤那些具备上网权限的应用
                    if (permission.equals("android.permission.INTERNET")) {
                        isQueryNetwork = true;
                        break;
                    }
                }
                // 类型为0表示所有应用,为1表示只要联网应用
                if (type == 0 || (type == 1 && isQueryNetwork)) {
                    AppInfo app = new AppInfo();
                    app.uid = item.uid; // 获取应用的编号
                    app.label = item.loadLabel(pm).toString(); // 获取应用的名称
                    app.package_name = item.packageName; // 获取应用的包名
                    app.icon = item.loadIcon(pm); // 获取应用的图标
                    appList.add(app);
                }
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return appList;  // 返回去重后的应用包队列
    }

By calling this method, we get a piece of data for all applications in the current mobile phone.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/114025159