【问题标题】:how can i make adb sendevent faster?我怎样才能让 adb sendevent 更快?
【发布时间】:2022-02-18 09:59:22
【问题描述】:

我使用 sendevent 来模拟复杂的手势(点击、滑动、长按并在最后滑动)。问题是sendevent很慢,cmd中每个sendevent命令0.1秒,点击需要7个sendevent命令。我非常了解 sendevent 会打开文件、写入、关闭等 7 次。如何优化?

我知道输入点击,但输入比发送事件慢

我在 python 上的代码:

import time
from ppadb.client import Client

adb = Client(host='127.0.0.1', port=5037)
devices = adb.devices()
if len(devices) == 0:
    print('Devices not found')
    quit()
device = devices[0]

def sendevent(_type, _code, _value, _deviceName='/dev/input/event4'):
    last_time = time.time()
    device.shell(f"su -c 'sendevent {_deviceName} {_type} {_code} {_value}'")
    print(time.time()-last_time)

def TapScreen(x, y):
    sendevent(EV_ABS, ABS_MT_ID, 0)
    sendevent(EV_ABS, ABS_MT_TRACKING_ID, 0)
    sendevent(1, 330, 1)
    sendevent(1, 325, 1)
    sendevent(EV_ABS, ABS_MT_PRESSURE, 5)
    sendevent(EV_ABS, ABS_MT_POSITION_X, x)
    sendevent(EV_ABS, ABS_MT_POSITION_Y, y)
    sendevent(EV_SYN, SYN_REPORT, 0)
    sendevent(EV_ABS, ABS_MT_TRACKING_ID, -1)
    sendevent(1, 330, 0)
    sendevent(1, 325, 0)
    sendevent(EV_SYN, SYN_REPORT, 0)

EV_ABS             = 3
EV_SYN             = 0
SYN_REPORT         = 0
ABS_MT_ID          = 47
ABS_MT_TOUCH_MAJOR = 48
ABS_MT_POSITION_X  = 53
ABS_MT_POSITION_Y  = 54
ABS_MT_TRACKING_ID = 57
ABS_MT_PRESSURE    = 58

TapScreen(1000, 500)

是否可以将所有操作写入一个文件,然后将它们作为一个发送事件一起发送?

我找到了问题的答案,现在的问题是在哪里运行代码? :D

回答:
https://stackoverflow.com/questions/18848540/rewrite-sendevent-c-from-c-to-java\ https://gist.github.com/Yhzhtk/6595875

请原谅我的英语,我用谷歌翻译:)

【问题讨论】:

标签: python android adb


【解决方案1】:

您正在为每个 sendevent 命令调用 adb shell sendevent …。如果您只需一次调用 adb shell 即可发送所有内容(将所有发送事件命令添加到同一个字符串,用 ; 分隔),那么它会运行得更快(但仍然有点慢)。

例如:adb shell “sendevent1 ; sendevent2 ; sendevent3 ; …”

【讨论】:

    猜你喜欢
    • 2011-06-14
    • 2012-03-23
    • 1970-01-01
    • 2020-04-06
    • 2017-09-07
    • 2021-11-12
    • 2014-07-05
    • 2021-07-09
    • 2020-12-27
    相关资源
    最近更新 更多