Android 靜默安裝/後台安裝

來源:互聯網
上載者:User

標籤:

  Android實現靜默安裝其實很簡單,今天在網上找資料找半天都說的很複雜,什麼需要系統安裝許可權、調用系統隱藏的api、需要系統內容下編譯、需要跟systemUI同進程什麼的。我不知道他們真的實現了靜默安裝沒有,反正我按照他們的方式統統都失敗了。

    下面我來說說我的靜默安裝實現方式,親測效果跟豌豆莢一樣,並且實現起來非常簡單:

    

    1.支援靜默安裝的機器必須Root,這個不需要我多講了。

    2.使用pm指令安裝即可。

    3.特別注意 PM指令不支援中文,也就說路徑中有中文會導致安裝失敗!

 

    關鍵代碼如下:

execRootCmdSilent("pm install -r " + Environment.getExternalStorageDirectory().getPath()+"/xxx.apk")

 1 public int execRootCmdSilent(String cmd) {   2        int result = -1;   3        DataOutputStream dos = null;   4    5        try {   6            Process p = Runtime.getRuntime().exec("su");   7            dos = new DataOutputStream(p.getOutputStream());   8    9            Log.i(TAG, cmd);  10            dos.writeBytes(cmd + "\n");  11            dos.flush();  12            dos.writeBytes("exit\n");  13            dos.flush();  14            p.waitFor();  15            result = p.exitValue();  16        } catch (Exception e) {  17            e.printStackTrace();  18        } finally {  19            if (dos != null) {  20                try {  21                    dos.close();  22                } catch (IOException e) {  23                    e.printStackTrace();  24                }  25            }  26        }  27        return result;  28    }  

    不需要在Manifest中聲明任何許可權

 

 

原文:http://blog.csdn.net/h3c4lenovo/article/details/9202323

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.