How to simulate input in Android Development


The main idea is to use ADB shell input instruction to simulate key and touch input.

However, the premise is that root is required, and Huawei mobile phones have stopped root decoding for security reasons. So the test suggests changing individual mobile phones. Or directly use the simulator in as and the version with Google APIs.

Input instruction

We open ADB, enter the shell, and enter input to see the parameter description of the instruction.

The default value of source is generally used and can be ignored. We mainly focus on the following command

  • Text: text input; Keyevent: keyboard key; These two instructions are common to all devices.
  • Tap: click the screen; Swipe: slide the screen; These two commands apply to devices with touch screens.
  • Press, roll is suitable for devices with touch balls.

Analog input

Before using the input instruction, we need to obtain the root permission.

private void execShellCmd(String cmd) { 
 try { 
 Process process = Runtime.getRuntime().exec("su"); 

 OutputStream outputStream = process.getOutputStream(); 
 DataOutputStream dataOutputStream = new DataOutputStream( 
 } catch (Throwable t) { 


1. You need to get the focus in advance before entering.
2. Enter special characters with special meanings, which cannot be directly entered. Keyevent needs to be used, such as: ‘”

We complete an EditText and then perform a text input test.

execShellCmd("input text 'hello,world'");

We found that an H is missing and the log can be seen on the console.

You can see that EditText does not get the focus when pressing H.

It may be that the input operation starts after the page is initialized. At this time, EditText has not obtained the focus, and there may be a delay in obtaining the focus. So we try to input after a delay of 1s.

private Handler handler = new Handler();
private Runnable task = new Runnable() {
 public void run() {
 execShellCmd("input text 'hello,world'");

//Input after 1s delay


execShellCmd("input text 'hello,world' \n input keyevent 68 \n input keyevent 21");

Type Hello, world, then ‘and move the cursor to the left

For common keycodes, see frameworks / base / core / Java / Android / view / keyevent java


The coordinate system in Android is shown in the figure below.

We can open the developer option – > pointer position in the mobile phone to assist in positioning, and you can see the relative offset of X and y at the top.

Click the position on the screen (100200).

execShellCmd("input tap 100 200");


Sliding screen is similar to tap. You only need to pass in two coordinates. The sliding time (MS) can also be set later. The shorter the time, the longer the corresponding sliding distance will be.

Slide from the screen (100200) to (300400).

execShellCmd("input swipe 100 200 300 400");

The above is the details of how to simulate input in Android development. For more information about Android analog input, please pay attention to other relevant articles of developeppaer!