如何在 Android 应用程序中运行终端命令? [英] How to run terminal command in Android application?
本文介绍了如何在 Android 应用程序中运行终端命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过android app向终端发送命令并返回输出?例如,发送ls/"并获取输出以在GUI中打印?
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.os.Exec.createSubprocess():
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 + "
";
}
}
catch (IOException e) {}
return output;
}
这篇关于如何在 Android 应用程序中运行终端命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文