分享下android下,靜默安裝的方法

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   color   os   sp   for   


代碼如下:

  1. /** 

  2.  * 靜默安裝 

  3.  * @param file 

  4.  * @return  

  5.  */  

  6. public boolean slientInstall(File file) {  

  7.     boolean result = false;  

  8.     Process process = null;  

  9.     OutputStream out = null;  

  10.     try {  

  11.         process = Runtime.getRuntime().exec("su");  

  12.         out = process.getOutputStream();  

  13.         DataOutputStream dataOutputStream = new DataOutputStream(out);  

  14.         dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "\n");  

  15.         dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " +  

  16.                 file.getPath());  

  17.         // 提交命令  

  18.         dataOutputStream.flush();  

  19.         // 關閉流操作  

  20.         dataOutputStream.close();  

  21.         out.close();  

  22.         int value = process.waitFor();  

  23.           

  24.         // 代表成功  

  25.         if (value == 0) {  

  26.             result = true;  

  27.         } else if (value == 1) { // 失敗  

  28.             result = false;  

  29.         } else { // 未知情況  

  30.             result = false;  

  31.         }  

  32.     } catch (IOException e) {  

  33.         e.printStackTrace();  

  34.     } catch (InterruptedException e) {  

  35.         e.printStackTrace();  

  36.     }  

  37.       

  38.     return result;  

  39. }  


注意代碼中pm install -r 前面的LD_LIBRARY_PATH=/vendor/lib:/system/lib,在4.0版本以上必須加上,不然會無法安裝。


分享下android下,靜默安裝的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.