问题遇到的现象和发生背景
安卓获取root权限使用process类获取的时候会卡上五六秒
process.exitValue()第一次也会返回为1而不是0正常退出的结果码
问题相关代码,请勿粘贴截图
//此处为获取的代码
execShellBoolean("chmod 777 " + context.getPackageCodePath());
public static boolean execShellBoolean(String script) {
Process process = null;
DataOutputStream dataOutputStream;
// DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
dataOutputStream.writeBytes(script + "\n");
dataOutputStream.flush();
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
dataOutputStream.close();
return (process.exitValue() == 0);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;
} finally {
// try {
// if (dataOutputStream != null) {
// dataOutputStream.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
process.destroy();
}
}
运行结果及报错内容
安卓获取root权限使用process类获取的时候会卡上五六秒
process.exitValue()第一次也会返回为1而不是0正常退出的结果码
测试环境为雷电模拟器4.0 安卓7.0
我的解答思路和尝试过的方法
先前以为是管道堵死的情况后来使用线程清空管道的缓存,照样还会卡上个几秒而且
process.exitValue()第一次也会返回为1而不是0正常退出的结果码
请问是什么原因,经过断点调试卡在位置应该是在process.waitFor()这个函数
但是明明已经执行成功且以及process.destroy();销毁了process但是一样会卡而且返回结果码一样为异常的1
我想要达到的结果
execShellBoolean("chmod 777 " + context.getPackageCodePath());
在使用这段命令的时候不造成卡顿且process.exitValue()返回的结果码为正常的0