今天试了一整天,有点崩溃..开发环境 Android studio 2.1,试了网络上范例
device 是 nexus 6P 安卓7.0
onCreate(){
TextView tex = (TextView)findViewById(R.layout.textView);
try {
execCommand("ls");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void execCommand(String command) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
BufferedReader in = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
stringBuffer.append(line+"-");
}
tex.setText(stringBuffer.toString());
} catch (InterruptedException e) {
System.err.println(e);
}
}
他可以印出执行完 "ls"后的字串,并展示在 app 上,其他
cat /proc/sys/net/ipv4/ip_forward 也可以展示出来