首页 / 操作系统 / Linux / Android执行Linux命令
Process localProcess = Runtime.getRuntime().exec("su");这条代码获得root权限OutputStream localOutputStream = localProcess.getOutputStream();DataOutputStream localDataOutputStream = new DataOutputStream(localOutputStream);从Process对象获得输出流,然后我们就可以执行Linux命令了localDataOutputStream.writeBytes(String str);其中str是String类型的变量。注意在str最后有换行例如 String str="mkdir /mnt/sdcard/zhycheng
";InputStream localInputStream = localProcess.getInputStream();DataInputStream localDataInputStream = new DataInputStream(localInputStream);这两条代码获得输出流例如 String out=localDataInputStream .ReadLine();这是从控制台输出的每输出之后调用localDataOutputStream.flush();还要调用localProcess .waitFor();通过调用测试命令int i = execRootCmdSilent("echo test");判断是否获得root权限,i不等于-1就获得了root权限