android 7.0 root下靜默安裝
阿新 • • 發佈:2018-12-16
公司產品基於7.0開發,需要實現覆蓋升級。之前的產品覆蓋升級基於5.1系統,在7.0上不起作用。網上參考了許多部落格。最終稀裡糊塗的成功了,簡單記錄。
核心程式碼只有一句
String command = "pm install -r -i 包名 --user 0 apk路徑";
execInstallCommand(new String[]{command})
public static void execInstallCommand(String[] commands) { Process process = null; BufferedReader successResult = null; BufferedReader errorResult = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); for (String command : commands) { if (command == null) { continue; } // donnot use os.writeBytes(commmand), avoid chinese charset error os.write(command.getBytes()); os.writeBytes(COMMAND_LINE_END); os.flush(); } os.writeBytes(COMMAND_EXIT); os.flush(); // get command result } catch (IOException e) { Log.e(TAG,e.getMessage()); } catch (Exception e) { Log.e(TAG,e.getMessage()); } finally { try { if (os != null) { os.close(); } if (successResult != null) { successResult.close(); } if (errorResult != null) { errorResult.close(); } } catch (IOException e) { Log.e(TAG, "execCommand: ", e); } if (process != null) { process.destroy(); } } }