预备知识:熟悉Android开发环境,linux/mac环境,设备推荐使用pixel。
写在前面
adb是Android提供的一个调试工具,能够方便地通过电脑控制Android设备,本文将使用该工具实现简单的自动化点击操作。在阅读此文章之前需要先搭建好Android开发环境,确认能够使用adb
命令,具体搭建流程可自行网上搜索。
环境准备
- Android设备上启用开发者模式和USB调试模式。可参考:developer.android.com/studio/debu…
- 使用数据线连接Android设备与电脑,并确认设备已连接(通过adb devices命令确认)。在
List of devices attached
下面能出现设备列表则表示连接成功。
~ $ adb devices
List of devices attached
HT7AB1A00782 device
获取屏幕坐标
进入“开发者选项”(developer options),打开获取屏幕点击位置,打开后即可知道需要点击的位置的坐标,如下图。
触发tap事件模拟点击
使用adb shell input swipe x y
命令进行点击,x
和y
参数为上一步骤得到的屏幕坐标。例如:点击(500,1000)
位置,运行:
adb shell input tap 500 1000
触发swip事件滑动屏幕
使用adb shell input tap x1 y1 x2 y2 duration
命令进行点击,x1
和y1
表示起点坐标,x2
和y2
表示滑动终点坐标,duration表示滑动时长,单位毫秒。例如,从(500,1000)
位置,滑动到(500,500)
,滑动0.5
秒运行:
adb shell input swipe 500 1000 500 500 500
该操作表示从下到上滑动。
触发text事件输入文本
使用adb shell input text <string>
命令进行点击,表示要输入的文本内容,例如想输入"hello world"的文本内容,运行:
adb shell input text "hello world"
实践案例
这里给出一个简单的案例:打开某音app,实现自动点赞播放视频。然后执行以下命令:
adb shell input tap 100 200
adb shell input text "username"
adb shell input tap 100 500
adb shell input text "password"
adb shell input tap 500 1000 //点击登录按钮坐标(500,1000)