- 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;
- /**
- * Starts the shell
- */
- public void start() throws IOException {
- builder = new ProcessBuilder("adb", "shell");
- adb = builder.start();
- // reads from the process output
- processInput = adb.getOutputStream();
- // sends to process's input
- processOutput = adb.getInputStream();
- // thread that reads process's output and prints it to system.out
- 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();
- }
- /**
- * Stop the shell;
- */
- 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();
- }
- }
自动化ADB SHELL
最新推荐文章于 2024-05-15 05:52:53 发布