Android靜默安裝軟體app代碼(測試可用)

來源:互聯網
上載者:User

android手機在獲得root許可權之後,可以調用命令的方式靜默安裝軟體,這一點體驗是很不錯,但是目前網路上關於android靜默安裝app的代碼均出自一人之手,其中有一個非常sb的bug,借用代碼的人居然都沒有發現,導致網路上幾乎所有關於android app靜默安裝的代碼都是錯誤的。

 代碼如下 複製代碼

new Thread() { 
    public void run() { 
    Process process = null; 
    OutputStream out = null; 
    InputStream in = null; 
    try { 
    // 請求root 
    process = Runtime.getRuntime().exec("su");  
    out = process.getOutputStream(); 
    // 調用安裝 
    out.write(("pm install -r " + currentTempFilePath + "
").getBytes()); 
    in = process.getInputStream(); 
    int len = 0; 
    byte[] bs = new byte[256]; 
    while (-1 != (len = in.read(bs))) { 
        String state = new String(bs, 0, len); 
        if (state.equals("Success
")) { 
           //安裝成功後的操作 
         } 
       } 
    } catch (IOException e) { 
        e.printStackTrace(); 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } finally { 
        try { 
            if (out != null) { 
                out.flush(); 
                out.close(); 
            } 
            if (in != null) { 
                in.close(); 
            } 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
  } 
}.start();

這個代碼實際上也可以運行起來,在root的手機中,也可以靜默安裝成功,但是代碼有問題就是bug,不知道哪一天這個有問題的代碼就會出現錯誤

下面是修改之後的代碼,修改了上面的bug

 代碼如下 複製代碼

new Thread() {
    public void run() {
        Process process = null;
        OutputStream out = null;
        InputStream in = null;
        try {
            // 請求root
            process = Runtime.getRuntime().exec("su");
            out = process.getOutputStream();
            // 調用安裝
            System.out.println(apkFile.getAbsolutePath());
            out.write(("pm install -r " + apkFile.getAbsolutePath() + "
").getBytes());
            in = process.getInputStream();
            int len = 0;
            int readLen = 0;
            byte[] bs = new byte[256];
            //讀出所有的輸出資料
            while (-1 != (readLen = in.read(bs))) {
                len = len + readLen;
                //如果讀的資料大於緩衝區。則停止
                if (len > bs.length) {
                    len -= readLen;
                    break;
                }
            }
            String state = new String(bs, 0, len);
            if (state.startsWith("Success")) {
            // 安裝成功後的操作
            } 
             else {
                //靜默安裝失敗,使用手動安裝
                installByUser();
            } 
         } catch (IOException e) {
             e.printStackTrace();
         } catch (Exception e) {
            e.printStackTrace();
         } finally {
            try {
                if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }
            } catch (IOException e) {
            e.printStackTrace();
            }
        }
    }
}.start();

修改的地方就在於從輸出資料流中讀取輸出資料,可以看出如果輸出的資料大於緩衝區大小(256),就會導致安裝後的操作多次執行,上面這段錯誤的代碼之所以可以運行,是在於輸出資料流的輸出為Success ,小於256,但是如果運行指令出錯,返回一大推的錯誤,那麼上面的那一段將讀取所有的資料,讀逐一進行比較。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.