Android靜默安裝應用和靜默解除安裝應用

葉應是葉發表於2018-02-24

這裡提供兩個Android系統下靜默安裝應用和靜默解除安裝應用的方法,在 5.1 版本系統上執行通過。當然,執行程式碼的應用需要有系統許可權,這就需要應用通過系統簽名檔案進行簽名了,需要使用到 platform.pk8 和 platform.x509.pem 兩個檔案,可以參照我的另一篇文章:Android 用 platform.pk8 和 platform.x509.pem 生成 keystore 系統簽名檔案

   /**
     * 靜默安裝App
     *
     * @param apkPath apk路徑
     * @return 是否安裝成功
     */
    public static boolean installApp(String apkPath) {
        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder errorMsg = new StringBuilder();
        try {
            process = new ProcessBuilder("pm", "install", "-r", apkPath).start();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                successMsg.append(s);
            }
            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s);
            }
        } catch (Exception e) {

        } finally {
            try {
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (Exception e) {

            }
            if (process != null) {
                process.destroy();
            }
        }
        //如果含有“success”單詞則認為安裝成功
        return successMsg.toString().equalsIgnoreCase("success");
    }

    /**
     * 靜默解除安裝App
     *
     * @param packageName 包名
     * @return 是否解除安裝成功
     */
    public static boolean uninstallApp(String packageName) {
        Process process = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder errorMsg = new StringBuilder();
        try {
            process = new ProcessBuilder("pm", "uninstall", packageName).start();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s = successResult.readLine()) != null) {
                successMsg.append(s);
            }
            while ((s = errorResult.readLine()) != null) {
                errorMsg.append(s);
            }
        } catch (Exception e) {

        } finally {
            try {
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (Exception e) {

            }
            if (process != null) {
                process.destroy();
            }
        }
        //如果含有“success”單詞則認為解除安裝成功
        return successMsg.toString().equalsIgnoreCase("success");
    }

    /**
     * 判斷應用是否存在
     *
     * @param context     上下文
     * @param packageName 包名
     * @return 是否存在
     */
    private boolean appExist(Context context, String packageName) {
        try {
            List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
            for (PackageInfo packageInfo : packageInfoList) {
                if (packageInfo.packageName.equalsIgnoreCase(packageName)) {
                    return true;
                }
            }
        } catch (Exception e) {

        }
        return false;
    }


相關文章