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