Execute Shell Command from Android

execute shell command from android

You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.

Try something like this:

try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
outputStream.flush();

outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}

Is it possible to execute Shell scripts from Android application

So far as I have tried, it works. I think it is supposed to, because that is how many Linux GUI apps do some of their work, by issuing shell commands.

To make sure, I tried issuing a vanilla command's output over to Logcat on an old, low-end, unrooted Android 6.0 phone, and it worked (working code below).

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] cmd = new String[]{"ls", "-la", "/"};

try {
// These two lines are what we care about
Process process = Runtime.getRuntime().exec(cmd);
InputStream iStream = process.getInputStream();

// This is how we check whether it works
tryWriteProcessOutput(iStream);
} catch (IOException e) {
e.printStackTrace();
}

}

private void tryWriteProcessOutput(InputStream iStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));

String output = "";
String line;

try {
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
}

Log.d("cmdOutput", output);
}
}

However, your mileage might vary wildly here. With so many Android manufacturers, I would expect different versions of the command shell on different devices, and thus I wouldn't expect every Android device to be able to run just any command I threw at it, unless it's a really common one.

Besides, you might also run into problems with system permissions with the commands themselves rather than the command shell (ie. busybox: Permission denied).

How do I listen for a response from shell command in android studio?

UPDATE : Solution is in Unable using Runtime.exec() to execute shell command "echo" in Android Java code :

Runtime.getRuntime.exec() doesn't execute a shell command directly,
it executes an executable with arguments. "echo" is a builtin shell
command. It is actually a part of the argument of the executable sh
with the option -c. Commands like ls are actual executables. You can
use type echo and type ls command in adb shell to see the
difference.

So final code is:

String[] cmdline = { "sh", "-c", "echo $..." }; 
Runtime.getRuntime().exec(cmdline);

cat is also executable from within Runtime.exec() without invoking sh

This is also analyzed in https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html?page=2 in paragraph Assuming a command is an executable program

The code in Execute shell commands and get output in a TextView is good although it uses a command that is executable directly (ls, see update above) :

try {
// Executes the command.
Process process = Runtime.getRuntime().exec("ls -l");

// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.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();

// Waits for the command to finish.
process.waitFor();

return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

Execute adb shell commands inside my android application

Instead of:

Process p = Runtime.getRuntime().exec("settings put global policy_control immersive.navigation=*");

try:

Process p = Runtime.getRuntime().exec(new String[]{"settings", "put", "global", "policy_control", "immersive.navigation=*");

exec expects the command to be an array of strings where the first string is the command and the rest are its arguments

Execute shell commands and get output in a TextView

You can run command and display command output into text as below :

public class MainActivity extends Activity {

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.cmdOp);
tv.setText("Output :"+"\n"+runAsRoot());
}

public String runAsRoot() {

try {
// Executes the command.
Process process = Runtime.getRuntime().exec("ls -l");

// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.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();

// Waits for the command to finish.
process.waitFor();

return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

Note : The "su" command does only run if the device is rooted. Otherwise it throws an exception.



Related Topics



Leave a reply



Submit