I’m performing the following code to execute linux commands in my android application that I’m creating:
public void RunAsRoot(String[] cmds){ Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"n"); } os.writeBytes("exitn"); os.flush(); }
I want to know if there is a way to know what the command is returning after it is executing. for example, if I do “ls” I would like to see what the command wold normally output.
Answer
try this code :
try { Process process = Runtime.getRuntime().exec("ls"); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); StringBuilder result=new StringBuilder(); String line = ""; while ((line = bufferedReader.readLine()) != null) { result.append(line); } TextView tv = (TextView)findViewById(R.id.textView1); tv.setText(result.toString()); } catch (IOException e) {}