How to send a command to the terminal through android app and get the output back? For example, sending "ls /" and getting the output to print it in the GUI?
+3
A:
You have to use reflection to call android.os.Exec.createSubprocess():
public String ls () {
Class<?> execClass = Class.forName("android.os.Exec");
Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
int[] pid = new int[1];
FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/ls", "/", null, pid);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
String output = "";
try {
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
catch (IOException e) {}
return output;
}
b0lt
2010-05-16 09:59:25
What is execClass?
Osama Gamal
2010-05-16 10:27:08
Oops, that'd be the reflected class.
b0lt
2010-05-17 10:50:51
A:
Different solutions could be found here: http://code.google.com/p/market-enabler/wiki/ShellCommands I've not tested them yet.
Osama Gamal
2010-05-16 15:00:08