Running shell commands from an application [Rooted]

In my application, I want to run several command line commands that interpret the output. These commands are essentially what will work on the root phone.

How can I do it?

+4
source share
2 answers

First make sure that the shell command you need is really available in Android. I ran into problems suggesting that you can do things like redirecting output with .

This method also works on undisturbed phones, I believe v2.2, but you should definitely check the API link.

try { Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName); BufferedReader reader = new BufferedReader( new InputStreamReader(nfiq.getInputStream())); int read; char[] buffer = new char[4096]; StringBuffer output = new StringBuffer(); while ((read = reader.read(buffer)) > 0) { output.append(buffer, 0, read); } reader.close(); chmod.waitFor(); outputString = output.toString(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } 

While this is probably not 100% necessary, it is a good idea for the process to wait for exec to complete with process.waitFor (), as you said you take care of the exit.

+5
source

First you need to make sure that you have busybox installed, as it will install a list of the most commonly used shell commands, and then use the following code to run the command.

 Runtime.getRuntime().exec("ls"); 
+2
source

All Articles