SoFunction
Updated on 2024-11-19

Python script for cell phone control using adb

I. adb related commands:

1. Shut down adb service: adb kill-server
2. Start the adb service adb start-server
3. Query all currently running devices adb devices
4. There may be more than one virtual device running in adb You can specify the virtual device to run -s virtual device name
5. reboot device adb reboot -- specify virtual device adb -s device name reboot
6. view log adb logcat clear log adb logcat -c
7. Go to the linux shell adb shell where the common linux commands cd cat and so on type su to get the name of the super administrator To determine if any of these commands are available go to the system/bin directory and you'll know.
8. pass in files to the device adb push local file remote directory

9. Copy files from device to local adb -s emulator-5556 pull /data/ d:/

10. Displays all currently running emulators:
    adb devices

1 Install the application:
   adb install -r

12. Get the files in the simulator:
    adb pull <remote> <local>

13. Write files to the simulator:
    adb push <local> <remote>

14. Enter the shell mode of the emulator:
    adb shell

15. Uninstall the apk package:
      adb shell
      cd data/app
      rm
     exit
      adb uninstall
      adb install -r

16. view adb command help information:
      adb help
17. Delete system applications:
adb remount (remounts the system partition to make it writable again).
      adb shell
      cd system/app
     rm

18. Obtain administrator privileges:
     adb root

19. Reproduction of documents:
Copy a file or directory to the device:
     adb push <source> <destination></destination></source>
e.g. adb push /sdcard/
Copy a file or directory from the device:
     adb pull <source> <destination></destination></source>
e.g. adb pull /sdcard/.

20. Get a list of the instances of the currently running device and the status of each instance:
    adb devices

21:adb shell input tap
  This command simulates an Android phone clicking at the screen coordinates (X,Y).

 22:adb shell input swipe 

This command simulates an Android phone swiping from screen coordinates (X1,Y1) to coordinates (X2,Y2).

23、uiautomator dump dump: creates an XML dump of current UI hierarchy This command is used to become the UI hierarchy of the current interface and display it in XML format . This allows you to get the position of each component

Note: If the PC wants to control multiple Android phones at the same time, you must add -s after adb

Example: adb -s 13b6e4c4 shell input tap 400 400

Indicates a simulated click event at the (400,400) coordinate position on the screen for the Android phone 13b6e4c4.

24. can see the device information means that the device has been successfully connected, the next command is adb install path + package name.apk

For example, if my installer is on the desktop, the command is adb install C:\Users\hyh\Desktop\

*** adb shell uiautomator dump /mnt/sdcard/window_dump.xml Get the UI information of the current interface of the cell phone, generate window_dump.xml
*** adb shell input text "123" input text

Examples:

1, open cmd, enter the current folder, enter the command adb devices to view the current devices connected to the computer (provided that the phone open usb debugging mode), you can view the phone has been successfully connected.

2, if the phone is successfully connected, enter the command adb shell input tap 100 100 , that is, click on the screen coordinates of (100, 100) point, if you do not know the need to click on the point of the specific location of the words can be set in the developer mode of the phone.

II. The adb emulates keystrokes:

1. For example, use adb shell input keyevent <keycode> command, different keycode can realize different functions, the complete list of keycode details seeKeyEvent, excerpts of which I find interesting are as follows:

keycode hidden meaning
3 HOME key
4 return key
5 Open the dialing application
6 hang up
24 increase the volume
25 lower the volume
26 power button
27 Take a picture (needs to be in the camera app)
64 Open your browser.
82 menu key
85 Play/Pause
86 stop playing
87 Play the next song.
88 Play the previous song.
122 Move the cursor to the beginning of the line or to the top of the list
123 Move the cursor to the end of the line or to the bottom of the list
126 Resume playback
127 pause (media player)
164 unmute
176 Open System Settings
187 Switching Applications
207 Open Contacts
208 Open the calendar.
209 Turn on the music.
210 Open the calculator.
220 Reduce screen brightness
221 Increase screen brightness
223 system hibernation
224 Light up the screen.
231 Turn on the voice assistant
276 If there is no wakelock, hibernate the system.

2. Some examples of the use of the input command

power button

Command:

adb shell input keyevent 26

The execution effect is equivalent to pressing the power button.

menu key

Command:

adb shell input keyevent 82

HOME key
Command:

adb shell input keyevent 3

return key
Command:

adb shell input keyevent 4

volume control
Increase the volume:

adb shell input keyevent 24

Lower the volume:

adb shell input keyevent 25

Mute:

adb shell input keyevent 164

Media Controls

Play/Pause:

adb shell input keyevent 85

Stop playing:

adb shell input keyevent 86

Play the next song:

adb shell input keyevent 87

Play the previous song:

adb shell input keyevent 88

Resume playback:

adb shell input keyevent 126

Pause the playback:

adb shell input keyevent 127

Light up/off screen

You can toggle the screen on and off by simulating the power button as described above, but if you explicitly want to light up or turn off the screen, then you can use the following method.

Light up the screen:

adb shell input keyevent 224

Turn off the screen:

adb shell input keyevent 223

Use python scripts to automatically run cmd commands.

Create a python file in the adb folder

import os
('adb shell input tap 100 100');

Run the script and find that it has the same effect as typing the same statement at the command line.

2, can also be used, the simplest way to use the following, set shell=True, it will not pop up cmd box

process = ('adb shell input tap 14 1402',shell=True)

Program Example:

#coding:utf-8
# Program function: can realize the automatic click of the video on the personal page of Jitterbug, so as to automatically increase the number of visitors
#Idea: switching between two videos in the Jitterbug homepage to click on them can result in increased visits
# Use ADB program, the screen coordinates of the video can be found using the adb shell uiautomator dump command to get the xml source code of the page.
#The following are the coordinates of the first and second videos on the Xiaomi Mi Mix 2 Shakeology homepage
#Drawbacks: running can not move the screen, the follow-up can be used to get the module ID number to click on the appropriate location
import time
import subprocess
i = 0
# The time between each operation depends on the phone configuration, the higher the configuration the shorter the time
sleep_time = 0.5 
while 1:
  #Setting shell=True with popen doesn't pop up the cmd box
  process = ('adb shell input tap 14 1402',shell=True)
  (sleep_time)
  process = ('adb shell input keyevent KEYCODE_BACK', shell=True)
  (sleep_time)
  process = ('adb shell input tap 375 1402', shell=True)
  (sleep_time)
  process = ('adb shell input keyevent KEYCODE_BACK', shell=True)
  (sleep_time)
  #('adb shell input tap 14 1402')
  #('adb shell input keyevent KEYCODE_BACK')
  #('adb shell input tap 375 1402')
  i+=1
  print str(i) + 'clicks have been completed' 

Implementation Principle

Hierarchy Viewer: Get the real-time UI information of the current cell phone, which is convenient to be used for automated testing of cell phones;

() in python or the Python os module: invoke system commands;

uiautomator tool: get information about interface controls;

adb command: to operate the phone;

This is the whole content of this article.