Java如何操作Android的adb shell
2016-02-25 20:20
369 查看
Android在操作一些命令的时候需要进入到adb shell,然后才可以执行其他的命令,那么如何做到这一点呢?就要把adb shell按一个Process执行,然后再去操作这个Process的输入和输出流。
代码如下:
这段代码是操作 cmd.exe的
try {
Process process = Runtime.getRuntime().exec("cmd.exe");
OutputStream outputStream = process.getOutputStream();
final InputStream inputStream = process.getInputStream();
new Thread(new Runnable(){
byte[] cache = new byte[1024];
public void run() {
System.out.println("listener started");
try {
while(inputStream.read(cache)!=-1){
System.out.println(new String(cache));
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
outputStream.write(new byte[]{'d','i','r','\n'});
//inputStream.
int i = process.waitFor();
System.out.println("i=" + i);
} catch (Exception e) {
e.printStackTrace();
}
这段代码是操作Android adb shell 的
try {
Process process = Runtime.getRuntime().exec("adb shell"); //adb shell
final BufferedWriter outputStream = new BufferedWriter(newOutputStreamWriter(process.getOutputStream()));
final BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
//这里一定要注意错误流的读取,不然很容易阻塞,得不到你想要的结果,
final BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
new Thread(new Runnable() {
String line;
public void run() {
System.out.println("listener started");
try {
while((line=inputStream.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
final BufferedReader br = newBufferedReader(new InputStreamReader(System.in));
public void run() {
System.out.println("writer started");
String line;
try {
while ((line =br.readLine()) != null) {
outputStream.write(line + "\r\n");
outputStream.flush();
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
int i = process.waitFor();
System.out.println("i=" + i);
} catch (Exception e) {
e.printStackTrace();
}
}
代码如下:
这段代码是操作 cmd.exe的
try {
Process process = Runtime.getRuntime().exec("cmd.exe");
OutputStream outputStream = process.getOutputStream();
final InputStream inputStream = process.getInputStream();
new Thread(new Runnable(){
byte[] cache = new byte[1024];
public void run() {
System.out.println("listener started");
try {
while(inputStream.read(cache)!=-1){
System.out.println(new String(cache));
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
outputStream.write(new byte[]{'d','i','r','\n'});
//inputStream.
int i = process.waitFor();
System.out.println("i=" + i);
} catch (Exception e) {
e.printStackTrace();
}
这段代码是操作Android adb shell 的
try {
Process process = Runtime.getRuntime().exec("adb shell"); //adb shell
final BufferedWriter outputStream = new BufferedWriter(newOutputStreamWriter(process.getOutputStream()));
final BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
//这里一定要注意错误流的读取,不然很容易阻塞,得不到你想要的结果,
final BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
new Thread(new Runnable() {
String line;
public void run() {
System.out.println("listener started");
try {
while((line=inputStream.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
final BufferedReader br = newBufferedReader(new InputStreamReader(System.in));
public void run() {
System.out.println("writer started");
String line;
try {
while ((line =br.readLine()) != null) {
outputStream.write(line + "\r\n");
outputStream.flush();
}
} catch (IOException e) {
//e.printStackTrace();
}
}
}).start();
int i = process.waitFor();
System.out.println("i=" + i);
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- 哪些晦涩难懂的shell命令都是什么的缩写?
- adb shell dumpsys命令
- linux系统shell变量入门
- 【sehll学习】linux运维一个简单shell脚本监控系统内存
- shell之磁盘容量检查
- shell之磁盘容量检查
- 自认为比较经典的shell程序
- shell脚本实例-交叉证认
- shell获取 linux 系统的位数
- powershell 工作流作业
- linux中java调用shell脚本
- ansible模块command、shell、raw、script
- xshell里面使用黑色背景时蓝色字体看不清楚的解决方法
- shell printf命令:格式化输出语句
- shell 中数学计算总结
- 如何在shell脚本中判断文件或者文件夹是否存在?
- 如何在shell脚本中判断文件或者文件夹是否存在?
- 如何在shell脚本中判断文件或者文件夹是否存在?
- Powershell工作流中的function和script
- shell的if语句