外壳/批处理脚本直接命令ADB壳 [英] shell/ batch scripting to direct commands to adb shell

查看:212
本文介绍了外壳/批处理脚本直接命令ADB壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个批处理(对胜利)和一个shell脚本为Linux将键和触摸事件上的Andr​​oid UI自动化。目前,在Windows批处理文件,我开始一个亚行外壳的每个事件,例如

I am trying to write a batch(for win) and a shell script for linux to automate key and touch events on a android UI. At the moment in a windows batch file I am starting a adb shell for each event for eg

    :again

adb shell am start -a android.intent.action.MAIN -n com.q.me.fui.activity/.InitActivity

sleep 15

adb shell sendevent /dev/input/event0 3 0 281
adb shell sendevent /dev/input/event0 3 1 70
adb shell sendevent /dev/input/event0 1 330 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 330 0
adb shell sendevent /dev/input/event0 0 0 0   
adb shell sendevent /dev/input/event0 1 330 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 330 0
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 0 0 0   

sleep 5

adb shell input keyevent 82
adb shell input keyevent 20
adb shell input keyevent 20
adb shell input keyevent 22
adb shell input keyevent 22
adb shell input keyevent 22
adb shell input keyevent 66

sleep 5

goto again

以上code是逸岸每次开始一个新的亚行的shell。我想避免这种情况。我想我的批处理脚本启动亚行外​​壳只有一次,我想路由的SendEvent等命令的子shell,即亚行外壳。

The above code is infact starting a new adb shell each time. I want to avoid this. I want my batch script to start the adb shell only once and I would like to route the sendevent and other commands to the subshell, ie the adb shell.

任何想法如何,我可以在Win批次和林shell脚本做到这一点。我已经尝试了几种选择,但他们不似乎工作:(

Any idea how I can do this in win batch and Lin shell script. I have tried a few options but they dont seem to work :(

在此先感谢。

推荐答案

我不知道很多有关批处理脚本或shell脚本,但我很快就能够快速地编写Java程序来做到这一点:

I don't know much about batch scripting or shell scripting, but I was quickly able to quickly write a java program to do this:

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)  {}
   }

   /**
    * Executes a command on the shell
    * @param adbCommand the command line.
    * e.g. "am start -a android.intent.action.MAIN -n com.q.me.fui.activity/.InitActivity" 
    */
   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();

      for(String arg : args)  {
         if(arg.startsWith("sleep"))   {
            String sleep = arg.split(" ")[1].trim();
            long sleepTime = Integer.parseInt(sleep) * 1000;
            Thread.sleep(sleepTime);
         }else {
            shell.execCommand(arg);
         }
      }

      shell.stop();
   }
}

只要你喜欢传递命令作为命令行参数在main方法执行,您可以使用这个类的shell脚本。

You can then use this class in a shell script as you like passing the commands to execute as command line arguments in your main method.

例如。下面是shell脚本:

e.g. Below is the shell script:

#!/bin/bash

java AndroidShell "am start -a android.intent.action.MAIN -n com.q.me.fui.activity/.InitActivity" \
"sleep 15" \
"sendevent /dev/input/event0 3 0 281" \
"sendevent /dev/input/event0 3 1 70" \
"sendevent /dev/input/event0 1 330 1" \
"sendevent /dev/input/event0 0 0 0" \
"sleep 10" \
"sendevent /dev/input/event0 1 330 0" \
"exit"

这篇关于外壳/批处理脚本直接命令ADB壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆