自动化ADB SHELL
来源:互联网 发布:菠菜源码架设视频教程 编辑:程序博客网 时间:2024/06/07 05:24
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.IOException;
-
- public class AndroidShell {
- private ProcessBuilder builder;
- private Process adb;
- private static final byte[] LS = "/n".getBytes();
-
- private OutputStream processInput;
- private InputStream processOutput;
-
- private Thread t;
-
-
-
-
- public void start() throws IOException {
- builder = new ProcessBuilder("adb", "shell");
- adb = builder.start();
-
-
- processInput = adb.getOutputStream();
-
-
- processOutput = adb.getInputStream();
-
-
- Thread t = new Thread() {
- public void run() {
- try {
- int c = 0;
- byte[] buffer = new byte[2048];
- while((c = processOutput.read(buffer)) != -1) {
- System.out.write(buffer, 0, c);
- }
- }catch(Exception e) {}
- }
- };
- t.start();
- }
-
-
-
-
- public void stop() {
- try {
- if(processOutput != null && t != null) {
- this.execCommand("exit");
- processOutput.close();
- }
- }catch(Exception ignore) {}
- }
-
-
- public void execCommand(String adbCommand) throws IOException {
- processInput.write(adbCommand.getBytes());
- processInput.write(LS);
- processInput.flush();
- }
-
- public static void main(String[] args) throws Exception {
- AndroidShell shell = new AndroidShell();
- shell.start();
- shell.execCommand("input keyevent '3' ");//HOME
-
- shell.stop();
- }
- }