美文网首页
Android-读取已安装应用列表

Android-读取已安装应用列表

作者: CentForever | 来源:发表于2021-01-27 00:29 被阅读0次

    title: Android-读取已安装应用列表
    date: 2019-12-02 11:54:53
    categories:
    - Android
    tags:
    - App


    本文将通过读取已安装应用列表,如何获取应用列表,和为什么要获取已安装应用列表展开。

    Android 读取已安装应用列表需要申请权限吗?

    https://www.jianshu.com/p/dee8bc1fb847

    Android 如何完整的获取到用户已安装应用列表

    https://blog.csdn.net/q384415054/article/details/72972405

    为什么安卓的APP都要读取手机中已安装的应用列表

    https://blog.csdn.net/u012888262/article/details/71248898

    结合上述三篇文章,选择已下折中方案:

    思路:

    1.使用adb指令获取包名

    private void runCommand() {
        try {
            Process process = Runtime.getRuntime().exec("adb shell pm list package -3");
            BufferedReader bis = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = bis.readLine()) != null) {
                System.out.println("runCommand, line=" + line);
            }
        } catch (IOException e) {
            System.out.println("runCommand,e=" + e);
        }
    }
    

    2.根据包名使用PackageManager获取app的实际名字

    private void getAppList() {
        PackageManager pm = getPackageManager();
        // Return a List of all packages that are installed on the device.
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo packageInfo : packages) {
            // 判断系统/非系统应用
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) // 非系统应用
            {
                System.out.println("getAppList, packageInfo=" + packageInfo.packageName);
            } else {
                // 系统应用        
            }
        }
    }
    

    3.结合使用

    private void runCommand() {
        try {
            PackageManager pm = getPackageManager();
            Process process = Runtime.getRuntime().exec("adb shell pm list package -3");
            BufferedReader bis = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = bis.readLine()) != null) {
                PackageInfo packageInfo = App.instance.getPackageManager().getPackageInfo(line.replace("package:", ""), PackageManager.GET_GIDS);
                System.out.println("runCommand, line=" + line+"packageInfo.packageName" + ":" + packageInfo.applicationInfo.loadLabel(pm).toString());
            }
        } catch (IOException e) {
            System.out.println("runCommand,e=" + e);
        }
    }
    

    相关文章

      网友评论

          本文标题:Android-读取已安装应用列表

          本文链接:https://www.haomeiwen.com/subject/wsoyzktx.html