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?
如何通过android app向终端发送命令,获取输出?例如,发送“ls /”并将输出打印到GUI中?
3 个解决方案
#1
11
You have to use reflection to call android.os.Exec.createSubprocess():
必须使用反射调用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;
}
#2
1
Different solutions could be found here: http://code.google.com/p/market-enabler/wiki/ShellCommands I've not tested them yet.
这里可以找到不同的解决方案:http://code.google.com/p/enabler/wiki/shellcommands,我还没有测试它们。
#3
1
Try this answer there is way to run shell commands on android programmatically https://*.com/a/3350332/2425851
尝试这个答案,有一种方法可以在android上以编程方式运行shell命令https://*.com/a/3350332/2425851
#1
11
You have to use reflection to call android.os.Exec.createSubprocess():
必须使用反射调用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;
}
#2
1
Different solutions could be found here: http://code.google.com/p/market-enabler/wiki/ShellCommands I've not tested them yet.
这里可以找到不同的解决方案:http://code.google.com/p/enabler/wiki/shellcommands,我还没有测试它们。
#3
1
Try this answer there is way to run shell commands on android programmatically https://*.com/a/3350332/2425851
尝试这个答案,有一种方法可以在android上以编程方式运行shell命令https://*.com/a/3350332/2425851