The Method of Using adb to Control Mobile Phone in Python Script

  • 2021-07-10 20:25:43
  • OfStack

1. adb related commands:

1. Turn off the 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 run in adb. You can specify a virtual device run-s virtual device name
5. Restart device adb reboot--Specify virtual device adb-s Device name reboot
6. View log adb logcat Clear log adb logcat-c
7. Enter linux, shell, adb, shell, cd, cat, etc. Enter su to get the super administrator name. To determine whether there are any commands entering system/bin directory, you will know
8. Pass in files to the remote directory of adb push local files in the device

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

10. Displays all emulators currently running:
adb devices

1 Install the application:
adb install-r 123. apk

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

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

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

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

16. View adb Command Help:
adb help
17. Delete the system application:
adb remount (remount the system partition to make it writable again).
adb shell
cd system/app
rm 123. apk

18. Get administrator rights:
adb root

19. Copy files:
Copy 1 file or directory to device:
adb push < source > < destination > < /destination > < /source >
For example: adb push update. zip/sdcard/
Copy 1 file or directory from the device:
adb pull < source > < destination > < /destination > < /source >
For example: adb pull/sdcard/update. zip.

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

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

22: adb shell input swipe

This command simulates the Android mobile phone sliding 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 form the UI layer of the current interface and display it in XML format. So you can get the location of each component

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

For example: adb-s 13b6e4c4 shell input tap 400 400

Represents a click event that simulates the coordinate position on the screen (400, 400) for this Android mobile phone 13b6e4c4.

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

For example, if my installation package is on the desktop, the command is adb install C:\ Users\ hyh\ Desktop\ XXX. apk

*** adb shell uiautomator dump/mnt/sdcard/window_dump. xml obtains UI information of the current interface of the mobile phone and generates 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 devices currently connected to the computer (provided that the mobile phone opens usb debugging mode), and you can view the successfully connected mobile phone.

2. If the mobile phone is successfully connected, enter the command adb shell input tap 100 100, which means clicking the point with coordinates (100, 100) on the screen. If you don't know the specific location of the point to be clicked, you can set it in the mobile phone developer mode.

2. adb analog key:

1. For example, use adb shell input keyevent < keycode > Command, different keycode can achieve different functions, the complete list of keycode see KeyEvent, quotes I find interesting as follows:

keycode 含义
3 HOME 键
4 返回键
5 打开拨号应用
6 挂断电话
24 增加音量
25 降低音量
26 电源键
27 拍照(需要在相机应用里)
64 打开浏览器
82 菜单键
85 播放/暂停
86 停止播放
87 播放下1首
88 播放上1首
122 移动光标到行首或列表顶部
123 移动光标到行末或列表底部
126 恢复播放
127 暂停播放
164 静音
176 打开系统设置
187 切换应用
207 打开联系人
208 打开日历
209 打开音乐
210 打开计算器
220 降低屏幕亮度
221 提高屏幕亮度
223 系统休眠
224 点亮屏幕
231 打开语音助手
276 如果没有 wakelock 则让系统休眠

2. One example of the use of the input command

Power key

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 bond
Command:


adb shell input keyevent 3

Return key
Command:


adb shell input keyevent 4

Volume control
Increase the volume:


adb shell input keyevent 24

Reduce the volume:


adb shell input keyevent 25

Mute:


adb shell input keyevent 164

Media control

Play/pause:


adb shell input keyevent 85

Stop playing:


adb shell input keyevent 86

Play the next one:


adb shell input keyevent 87

Play the last one:


adb shell input keyevent 82
0

Resume playback:


adb shell input keyevent 82
1

Pause playback:


adb shell input keyevent 82
2

To light/extinguish the screen

You can switch the screen on and off by using the analog power button described above, but if you explicitly want to turn the screen on or off, you can use the following methods.

Light up the screen:


adb shell input keyevent 224

Turn off the screen:


adb shell input keyevent 82
4

3. Use the python script to run the cmd command automatically

Create an python file under the adb folder


adb shell input keyevent 82
5

Running the script has the same effect as typing the same statement on the command line.

2. You can also use subprocess. Popen. The simplest way to use it is as follows. If you set shell=True, the cmd box will not pop up


adb shell input keyevent 82
6

Program example:


adb shell input keyevent 82
7

Implementation principle

Hierarchy Viewer: Get the real-time UI information of the current mobile phone, which is convenient for automatic testing of mobile phones;

subprocess. Popen () or Python os module in python: Invoke system commands;

uiautomator tool: Obtain interface control information;

adb command: Operate the mobile phone;


Related articles: