You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需Root权限,如何用ADB模拟精细触摸操作并重放录制事件?

无需Root重放ADB getevent抓取的触摸事件方案

方案1:利用adb shell input的细粒度触摸命令

你可能不知道input命令除了tap和swipe,还有针对触摸屏的细粒度操作子命令,无需Root即可使用:

  • adb shell input touchscreen down <x> <y>:模拟手指按下
  • adb shell input touchscreen move <x> <y>:模拟手指移动
  • adb shell input touchscreen up:模拟手指抬起

操作步骤:

  1. 解析你录制的touch_events.txt文件,提取关键事件:
    • 找到EV_KEY BTN_TOUCH DOWN对应的坐标(从EV_ABS ABS_MT_POSITION_X/Y字段获取),对应执行down命令
    • 后续的EV_ABS ABS_MT_POSITION_X/Y事件对应执行move命令
    • EV_KEY BTN_TOUCH UP对应执行up命令
    • 保留事件间的时间间隔(从-t后的时间戳计算差值),用sleep命令模拟操作延迟
  2. 将解析后的事件转换成连续的input touchscreen命令序列,批量执行即可。

示例脚本片段:

sleep 0.1
adb shell input touchscreen down 500 800
sleep 0.05
adb shell input touchscreen move 550 850
sleep 0.05
adb shell input touchscreen move 600 900
sleep 0.1
adb shell input touchscreen up

方案2:借助AccessibilityService辅助功能

Android的辅助服务可以在无需Root的情况下模拟精细手势,适合复杂操作的重放:

  1. 编写一个简单的Android应用,注册AccessibilityService(需在Manifest中声明权限,并重写onAccessibilityEvent等核心方法)
  2. 在服务中实现dispatchGesture方法,将录制的触摸事件转换成GestureDescription对象(包含按下、移动、抬起的路径和时间点)
  3. 通过广播、本地文件或ADB命令触发服务执行手势重放。

优势:可以完美还原连续触摸和精细手势,支持多点触控;缺点是需要用户手动在系统设置中开启辅助权限,且需要基础Android开发能力。

方案3:使用MonkeyRunner工具

MonkeyRunner是Android SDK自带的自动化工具,无需Root即可发送触摸事件:

  1. 安装Android SDK并配置环境变量
  2. 编写Python脚本读取touch_events.txt,解析事件的坐标、时间戳和动作类型
  3. 调用MonkeyRunner的API执行触摸动作:
    • device.touchDown((x, y)):模拟按下
    • device.touchMove((x, y)):模拟移动
    • device.touchUp((x, y)):模拟抬起
    • 用MonkeyRunner.sleep(时间差)控制事件间隔

示例脚本片段:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

device = MonkeyRunner.waitForConnection()
# 读取并解析touch_events.txt
with open('touch_events.txt', 'r') as f:
    lines = f.readlines()
    # 此处省略解析逻辑,假设得到events列表,每个元素包含delay、action、x、y
    for event in events:
        MonkeyRunner.sleep(event['delay'])
        if event['action'] == 'down':
            device.touchDown((event['x'], event['y']))
        elif event['action'] == 'move':
            device.touchMove((event['x'], event['y']))
        elif event['action'] == 'up':
            device.touchUp((event['x'], event['y']))

内容的提问来源于stack exchange,提问作者Lightning's Master

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 21:35:26