代碼以下:spa
/** .net
* 靜默安裝 orm
* @param file get
* @return it
*/ io
public boolean slientInstall(File file) { class
boolean result = false; file
Process process = null; im
OutputStream out = null; margin
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "\n");
dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " +
file.getPath());
// 提交命令
dataOutputStream.flush();
// 關閉流操做
dataOutputStream.close();
out.close();
int value = process.waitFor();
// 表明成功
if (value == 0) {
result = true;
} else if (value == 1) { // 失敗
result = false;
} else { // 未知狀況
result = false;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
注意代碼中pm install -r 前面的LD_LIBRARY_PATH=/vendor/lib:/system/lib,在4.0版本以上必須加上,否則會沒法安裝。