阅读帖子后,我想出了如何模拟触摸事件:
adb shell input tap 100 100
我已经安装了 MagicMarker 以查看是否绘制了任何内容,但什么也没有出现。
我也尝试过使用 monkeyrunner/androidViewClient Touch 函数:
device.touch(100 , 100, 'DOWN_AND_UP');
我的 AndroidViewClient 的全部代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import getopt, sys
import os
# Add android to path becayuse it seems to not appear on windows
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools/lib")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/platform-tools")
# PyDev sets PYTHONPATH, use it
try:
for p in os.environ['PYTHONPATH'].split(':'):
if not p in sys.path:
sys.path.append(p)
except:
pass
try:
sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass
from com.dtmilano.android.viewclient import ViewClient, ViewNotFoundException
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device, serialno)
device.touch(100,100,"DOWN_AND_UP")
我成功地使用了 device.press("KEYCODE_MENU", "DOWN_AND_UP") 或 device.takeSnapshot(),我完全不明白为什么我的手机没有收到触摸事件。
顺便说一下,我使用的是真实设备(4.3 中的 GS3 和 GS4)
请随时询问更多信息。
最佳答案
我已经使用这个简单的 AndroidViewClient 测试了 MagicMarker 脚本。
请注意,版本 5.1.1 中引入了对 adbclient.drag()
的一些修复,因此请确保您拥有最新版本。
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2014 Diego Torres Milano
'''
__requires__ = ["androidviewclient >= 5.1.1"]
import pkg_resources
from com.dtmilano.android.adb.adbclient import AdbClient
AdbClient(serialno='.*').drag((100, 100), (400, 400), 1000)
这会产生:
另请注意,在您的脚本中,device
是 AdbClient
实例。
如果您查看 AdbClient
中的 drag()
是如何实现的,您会发现它正在使用带有参数的 input swipe
相应的 API 级别:
def drag(self, (x0, y0), (x1, y1), duration, steps=1):
'''
Sends drag event (actually it's using C{input swipe} command.
@param (x0, y0): starting point
@param (x1, y1): ending point
@param duration: duration of the event in ms
@param steps: number of steps (currently ignored by @{input swipe}
'''
version = int(self.getProperty('ro.build.version.sdk'))
if version <= 15:
raise RuntimeError('drag: API <= 15 not supported (version=%d)' % version)
elif version <= 17:
self.shell('input swipe %d %d %d %d' % (x0, y0, x1, y1))
else:
self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration))
关于android - 在 Android 上模拟的触摸、滑动或拖动事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22381143/