Android 靜默升級,靜默安裝

      實現靜默安裝首先手機root權限或者是system 應用

         在Android 4.4版本中,靜默升級代碼如下


    // 靜默安裝,1-安裝成功,或沒有升級文件,2-升級安裝出現異常,-1-程序異常
    public static int installBySlient(Context context, String filePath) {
        int result = 0;
        try {
            File file = new File(filePath);
            if (filePath == null || filePath.length() == 0
                    || (file = new File(filePath)) == null
                    || file.length() <= 0 || !file.exists() || !file.isFile()) {
                return 1;
            }

            String[] args = { "pm", "install", "-r", filePath };
            ProcessBuilder processBuilder = new ProcessBuilder(args);

            Process process = null;
            BufferedReader successResult = null;
            BufferedReader errorResult = null;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder errorMsg = new StringBuilder();

            try {
                process = processBuilder.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 (IOException e) {
                e.printStackTrace();
                result = 2;
            } catch (Exception e) {
                e.printStackTrace();
                result = 2;
            } finally {
                try {
                    if (successResult != null) {
                        successResult.close();
                    }
                    if (errorResult != null) {
                        errorResult.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (process != null) {
                    process.destroy();
                }
            }

            if (successMsg.toString().contains("Success")
                    || successMsg.toString().contains("success")) {
                result = 1;
            } else {
                result = 2;
            }

            logd("App升級信息:" + "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }

  但是在Android 7.0版本,使用這個方法會報空指針異常

   只需要修改一行代碼

  String[] args = { "pm", "install","-i ","xxxxxx", "-r",filePath };

    -i 後面的參數是應用報名

  

   還有一種場景,就是你的應用幫別的應用實現靜默升級,這裏的包名還是用你的應用包名


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章