脚本adb shell

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(); 
   } 
} 

 

#!/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" 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值