Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:08:41+00:00 2026-05-16T05:08:41+00:00

I am trying to write a batch(for win) and a shell script for linux

  • 0

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

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.

Any idea how I can do this in win batch and Lin shell script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:08:41+00:00Added an answer on May 16, 2026 at 5:08 am

    I don’t know much about batch scripting or shell scripting, but I was 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();
       }
    }
    

    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.

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a batch script (CMD @ Windows XP Pro) that will
I am trying to write a windows batch script that it will search until
I am trying to write a windows batch file that will print and execute
I'm trying to write a windows batch file that can delete files from subdirectories.
I am trying to write a .bat file to automate some shell commands. Most
I've got this little batch file I'm trying to write for a Windows 7
I'm trying to write a batch script, this script is responsible to launch a
I am trying to write a batch file so I can use TortoiseHg Annotate
Im trying to write a batch file which scans several files and looks for
I'm trying to write a batch file to xcopy a folder to a removable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.