Computer uses python to control Android phone

1. Download and install SDK Platform Tools on your computer

Insert image description here
Insert image description here
Downloaded file: platform-tools_r30.0.4-windows.zip (about 12M)
Then decompress the file to the specified directory.
Insert image description here
Decompress the path and file, then add system environment variables to the tool directory
Insert image description here
to verify the installation results.

#执行命令
adb version

Insert image description here
Verify installation results

After the mobile phone is connected to the computer USB, execute adb devices to check the mobile phone connection status.

Query connected devices/emulators: adb devices

To connect a mobile phone here, the mobile phone needs to enable USB debugging in developer mode. By the way, also enable the simulated button function, which will be used later.

The following problems often occur with this command:

offline - indicates that the device has not successfully connected or is unresponsive;

device ——The device is connected;

no device - no device/emulator connection;

List of devices attached The device/emulator is not connected to adb or is unresponsive

A simple way to get the coordinates of the mobile phone screen

Method 1. Enter the developer mode of the phone, turn on the function of displaying coordinates in real time on the phone, and long press the screen position to automatically display the coordinates.

Method 2. Use the android adb shell command to obtain

# 截屏到手机
adb shell screencap /sdcard/screen.png
# 将手机上刚才的截图上传到电脑
adb pull /sdcard/screen.png  /Users/Administrator/Desktop/screen.png

From the original picture, use PS to deduct the partial picture you want, and then use the following code to get the coordinates of the partial picture on the original picture. If you don't know how to use PS, you can directly
use screenshots to take screenshots on the original picture, but it is not as precise as PS.

import aircv as ac

# 根据图片在图片上查找坐标
# imgsrc=原始图像,imgobj=待查找的图片,confidence=设置匹配系数
def matchImg(imgsrc, imgobj, confidence=0.2):
    imsrc = ac.imread(imgsrc)
    imobj = ac.imread(imgobj)
    match_result = ac.find_all_template(imsrc, imobj, confidence)
    return match_result


if __name__ == '__main__':
    p = matchImg("C:\\Users\\Administrator\\Desktop\\screen.png", "C:\\Users\\Administrator\\Desktop\\daicha.png")
    print(p)

Use python to control mobile phones

import os
import time


def execute(cmd):
    adbstr = 'adb shell {}'.format(cmd)
    print(adbstr)
    os.system(adbstr)


if __name__ == '__main__':

    while True:
        # 点击位置一
        execute("input tap 350 2200")
        time.sleep(3)
      
        # 点击位置二
        execute("input tap 970 135")
        time.sleep(5)

adb simulates key operations

1. Simulate text input operation

## 模拟输入abc
adb shell input text abc

2. Simulate key operations through key values

## 模拟按back键
adb shell input keyevent 4
## 或者
adb shell input keyevent KEYCODE_BACK

3. Simulate click operations through coordinates

## 点击横坐标300,纵坐标500的点
adb shell input tap 300 500

4. Simulate sliding operation through coordinates

## 从(200,300)的点划到(500,300)的点,滑动时间100ms
adb shell input swipe 200 300 500 300 100

5. Simulate drag operation through coordinates

## 从(100,1220)的点拖动到(500,620)的点,滑动时间2000ms
adb shell input draganddrop 100 1220 500 620 2000

6. Simulate long press sliding operation through coordinates ( note the difference from sliding operation )

Press and hold, and swipe from the point (200,300) to the point (201,301). The sliding time is 2000ms. Since the swiping distance is short and the time is long, it is subjectively displayed as a long press operation within an icon.

Items in the game backpack can be dragged and thrown into the warehouse.

adb shell input swipe 200 300 201 301 2000

7. Simulate physical key long press operation

Get the entity key value through getevent &


/dev/input/event4: 0001 0074 00000001
/dev/input/event4: 0000 0000 00000000
/dev/input/event4: 0001 0074 00000000
/dev/input/event4: 0000 0000 00000000

0074 is the power key value, converted to decimal 116, write the following script

sendevent /dev/input/event4 1 116 1
sendevent /dev/input/event4 0 0 0
sleep 4
sendevent /dev/input/event4 1 116 0
sendevent /dev/input/event4 0 0 0

8. Simulate pressing two keys at the same time through coordinates/key values ​​and connect them with and

## 电源键亮屏并滑动解锁
adb shell input keyevent KEYCODE_POWER and KEYCODE_MENU

android adb shell common commands

android adb shell official command (English) https://adbshell.com/

The following command source: https://www.jianshu.com/p/ccd35afa8470

1. Simulate click

adb shell input tap 100 100

2. Slide

adb shell input swipe x1 y1 x2 y2 
adb input touchscreen swipe x1 y1 x2 y2 100

adb shell input swipe 100 100 400 100  300 #左往右
adb shell input swipe 400 100 100 100  300 #右往左
adb shell input swipe 100 100 100 400  300 #上往下
adb shell input swipe 100 400 100 100  300 #下往上
adb shell input swipe 100 100 400 400  300 #上往下斜
adb shell input swipe 400 400 100 100  300 #下往上斜

3. Long press

adb shell input swipe 100 100 100 100  1000 //在 100 100 位置长按 1000毫秒

adb shell input swipe 367 469 367 469 800

4.Print all package names

adb shell pm list packages

➜  ~ adb shell pm list packages
package:com.huawei.floatMms
package:com.android.defcontainer
package:com.tencent.mm

5. Print the apk path of the specified package

adb shell pm path com.android.phone

➜  ~ adb shell pm path com.huawei.android.launcher
package:/system/app/HwLauncher6.apk

6. Delete the customized package

adb shell pm clear com.test.abc

7. Screenshot

adb shell screencap /sdcard/screen.png
adb pull /sdcard/screen.png #下载到本地

8. Get clicked location information

adb shell getevent

>
/dev/input/event0 3 39 3e1
/dev/input/event0 1 14a 1
/dev/input/event0 1 145 1
/dev/input/event0 3 35 406  //x坐标
/dev/input/event0 3 54 1083  //y坐标
/dev/input/event0 0 0 0
/dev/input/event0 3 39 ffffffff
/dev/input/event0 1 14a 0
/dev/input/event0 1 145 0
/dev/input/event0 0 0

getevent -l -c 16
输出所有event设备的基本信息

add device 1: /dev/input/event2
  name:     "hi6421_on"
could not get driver version for /dev/input/mouse0, Not a typewriter
add device 2: /dev/input/event4
  name:     "huawei,touchscreen"
add device 3: /dev/input/event0
  name:     "mhl_rcp_dev"
could not get driver version for /dev/input/mice, Not a typewriter
add device 4: /dev/input/event1
  name:     "hisi_gpio_key.14"
add device 5: /dev/input/event3
  name:     "hi3630_hi6401_CARD Headset Jack"

getevent -c 10 //输出10条信息后退出
getevent -l  //将type、code、value以对应的常量名称显示

9. Open the corresponding activity

adb shell am start -n {包(package)名}/{包名}.{
    
    活动(activity)名称}

adb shell am start com.songheng.eastnews/com.oa.eastfirst.activity.WelcomeActivity

10. Obtain the information of the current active window, package name and active form

adb shell dumpsys window windows | grep mCurrent 

11. Package name management command to obtain the corresponding apk path of the corresponding package name

adb shell pm  path com.migu.lobby

12. Use the dumpsys command to view the activities currently running on your Android phone

adb shell dumpsys activity activities | findstr "Run"

13. Use uiautomator dump to obtain page elements on the app

adb shell uiautomator dump /data/local/tmp/uidump.xml
adb shell uiautomator dump /sdcard/dump.xml

14. Download files

adb pull /sdcard/demo.mp4 

15.Upload files

adb push test.apk /sdcard

16. Rest screen

adb shell input keyevent 26

17.keyevent

adb shell input keyevent 20 #向下

adb shell input keyevent 4 #返回

adb shell input keyevent 3 #Home

adb shell input keyevent 6 #挂机

adb shell input keyevent 84 #搜索

adb shell input keyevent 26 #电源

adb shell input keyevent 24 #音量+

adb shell input keyevent 25 #音量-

18. Input box input

adb shell input text "ANDROID"

19. Use wireless to view adb shell

> adb tcpip 5555

连接:
> adb connect IP:5555

20. View all connected devices

adb devices

21. Installation and uninstallation

adb install <apk文件路径>
adb install -r <apk文件路径>     通过install命令来安装apk文件,-r参数可以重新安装某个应用并保留应用数据

adb install -r ~/chrome.apk

卸载应用:
adb uninstall <软件名>
adb uninstall -k < 软件名>         如果加 -k 参数,为卸载软件但是保留配置和缓存文件

adb uninstall com.android.chrome

22.Shutdown command

adb shell
su
reboot -p

Guess you like

Origin blog.csdn.net/lojloj/article/details/117065725