android 我怎样才能使adb发送得更快?

5t7ly7z5  于 2023-01-07  发布在  Android
关注(0)|答案(1)|浏览(362)

我使用sendevent来模拟复杂的手势(点击,滑动,长时间点击并在最后滑动)。问题是sendevent相当慢,cmd中每个sendevent命令0. 1秒,点击需要7个sendevent命令。我非常理解sendevent打开文件,写入,关闭等7次。如何优化它?
我知道输入抽头,但输入比sendevent慢
我在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)

是否可以将所有操作写入一个文件,然后将它们作为一个发送事件一起发送?
原谅我的英语,我用谷歌翻译:)

2ic8powd

2ic8powd1#

您正在为每个sendevent命令调用adb shell sendevent …。如果您只调用一次adb shell就发送了所有的(将所有send events命令添加到同一个字符串中,用;)那么它会跑得更快(但还是有点慢)。
例如:adb shell “sendevent1 ; sendevent2 ; sendevent3 ; …”

相关问题