Android在代码中执行shell命令,针对root命令
2017-09-04 17:08
351 查看
public void runRootCommand(String command) { Process process = null; DataOutputStream dataOutputStream = null; DataInputStream dataInputStream = null; StringBuffer wifiConf = new StringBuffer(); try { process = Runtime.getRuntime().exec("su"); dataOutputStream = new DataOutputStream(process.getOutputStream()); dataInputStream = new DataInputStream(process.getInputStream()); dataOutputStream .writeBytes(command+"\n"); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); InputStreamReader inputStreamReader = new InputStreamReader( dataInputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader( inputStreamReader); String line = null; while ((line = bufferedReader.readLine()) != null) { wifiConf.append(line); } bufferedReader.close(); inputStreamReader.close(); process.waitFor(); Log.d("shell命令执行结果:",process.exitValue()+""); } catch (Exception e) { e.printStackTrace(); } finally { try { if (dataOutputStream != null) { dataOutputStream.close(); } if (dataInputStream != null) { dataInputStream.close(); } process.destroy(); } catch (Exception e) { e.printStackTrace(); } } }
process.exitValue()返回0时表明shell命令执行成功
相关文章推荐
- android代码执行adb shell终端命令(linux命令)并返回执行结果
- Android中执行java命令的方法及java代码执行并解析shell命令
- Android java代码中如何执行shell命令
- android 执行shell命令代码
- Android中级篇之用JAVA代码执行shell命令
- Android中级篇之用JAVA代码执行shell命令
- Android中使用代码执行shell命令
- Android或Java应用中如何以默认用户或root用户执行Shell命令
- android执行shell命令
- Android执行shell命令
- Android执行shell命令
- Android执行执行Shell命令总结
- Android 执行 adb shell 命令
- 破解android的root权限的本质是:在系统中加入一个任何用户都可能用于登陆的su命令。或者说替换掉系统中的su程序,因为系统中的默认su程序需要验证实际用户权限,只有root和 shell用户才
- Android执行shell命令
- android应用程序执行shell脚本命令
- Android执行shell命令
- android上代码去执行"su"命令
- Android执行shell命令
- 处理非完全root手机,在代码层批量执行有关联的命令