80,471
社区成员




public static void execCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
Log.e("*** DEBUG ***", "111111111111111111");
process = Runtime.getRuntime().exec("su");
Log.e("*** DEBUG ***", "222222222222222222");
InputStream inputstream = process.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
Log.e("*** DEBUG ***", "3333333333333333331");
os = new DataOutputStream(process.getOutputStream());
//os.writeBytes(command+"\n");
os.writeBytes("ls \n");
os.writeBytes("exit\n");
os.flush();
Log.e("*** DEBUG ***", "444444444444444444444");
String line = "";
StringBuilder sb= new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
sb.append(line);
sb.append('\n');
}
//////////////
process.waitFor();
Log.e("*** DEBUG ***", "555555555555555 - " + sb.toString());
} catch (Exception e) {
Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+e.getMessage());
//return false;
}
}
05-29 00:50:56.967: E/*** DEBUG ***(6244): 111111111111111111
05-29 00:50:57.060: E/*** DEBUG ***(6244): 222222222222222222
05-29 00:50:57.061: E/*** DEBUG ***(6244): 3333333333333333331
05-29 00:50:57.062: E/*** DEBUG ***(6244): 444444444444444444444
05-29 00:50:57.065: E/*** DEBUG ***(6244): 555555555555555 -
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) {
execCommand("ping www.baidu.com");
}
public static void execCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(command);
InputStream inputstream = process.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(
inputstream);
BufferedReader bufferedreader = new BufferedReader(
inputstreamreader);
os = new DataOutputStream(process.getOutputStream());
// os.writeBytes(command+"\n");
os.writeBytes("ls \n");
os.writeBytes("exit\n");
os.flush();
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
sb.append(line);
System.out.println(line);
sb.append('\n');
}
process.waitFor();
} catch (Exception e) {
}
}
}
执行结果 :
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_req=1 ttl=56 time=12.8 ms
64 bytes from 61.135.169.125: icmp_req=2 ttl=56 time=157 ms
64 bytes from 61.135.169.125: icmp_req=3 ttl=56 time=12.2 ms
64 bytes from 61.135.169.125: icmp_req=4 ttl=56 time=12.6 ms
64 bytes from 61.135.169.125: icmp_req=5 ttl=56 time=281 ms
64 bytes from 61.135.169.125: icmp_req=6 ttl=56 time=972 ms
64 bytes from 61.135.169.125: icmp_req=7 ttl=56 time=35.0 ms
64 bytes from 61.135.169.125: icmp_req=8 ttl=56 time=59.5 ms
64 bytes from 61.135.169.125: icmp_req=9 ttl=56 time=393 ms
64 bytes from 61.135.169.125: icmp_req=10 ttl=56 time=138 ms
64 bytes from 61.135.169.125: icmp_req=11 ttl=56 time=954 ms
代码是没问题的 看看手机里面有没有你需要的这些命令