因为个人习惯原因、习惯性的把他写成静态方法、存放在工具类里面、方便直接调用、现在把该方法提出来、可直接通过命令来实现静默安装(pm install *.apk)、
/**
* 请求ROOT权限后执行命令(最好开启一个线程)
* @param cmd(pm install -r *.apk)
* @return
*/
public static boolean runRootCommand(String cmd) {
Process process = null;
DataOutputStream os = null;
BufferedReader br = null;
StringBuilder sb = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd+"\n");
os.writeBytes("exit\n");
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
sb = new StringBuilder();
String temp=null;
while((temp = br.readLine())!=null){
sb.append(temp+"\n");
if("Success".equalsIgnoreCase(temp)){
LogUtils.logE("----------"+sb.toString());
return true;
}
}
process.waitFor();
} catch (Exception e) {
LogUtils.logE("异常:"+e.getMessage());
}
finally {
try {
if (os != null) {
os.flush();
os.close();
}
if(br!=null){
br.close();
}
process.destroy();
} catch (Exception e) {
return false;
}
}
return false;
}
ps:LogUtils.logE();方法只是将Log.E();进行了再次的封装、
希望通过这段代码可以举一反三、在获取root权限下、实现静默安装卸载也就都只是命令而已了、