如何使用Android模拟触摸事件,同时手动输入X和Y坐标?如何在Android中模拟触摸事件?
回答
你应该给新的monkeyrunner一个去。也许这可以解决你的问题。你把键码放在里面进行测试,也许触摸事件也是可能的。
请让我知道如何安装monkeyrunner。 adb不识别monkeyrunner – indira 2010-12-10 04:24:08
这与adb的ui excersiser猴子无关。你会在adt的版本9的工具目录中找到monkeyrunner。 – keyboardsurfer 2010-12-10 11:38:05
同时查看我在答案中提供的链接。这导致Google提供的有关monkeyrunner的信息。 – keyboardsurfer 2010-12-10 12:26:51
如果我理解清楚,你想以编程方式做到这一点。然后,您可以使用方法View
,并用您需要的坐标创建MotionEvent
。
MotionEvent仅通过触摸屏幕生成。
这是一个monkeyrunner脚本,用于将触摸和拖动操作发送到应用程序。我一直在使用它来测试我的应用程序可以处理快速重复的滑动手势。
# This is a monkeyrunner jython script that opens a connection to an Android
# device and continually sends a stream of swipe and touch gestures.
#
# See http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html
#
# usage: monkeyrunner swipe_monkey.py
#
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connects to the current device
device = MonkeyRunner.waitForConnection()
# A swipe left from (x1, y) to (x2, y) in 2 steps
y = 400
x1 = 100
x2 = 300
start = (x1, y)
end = (x2, y)
duration = 0.2
steps = 2
pause = 0.2
for i in range(1, 250):
# Every so often inject a touch to spice things up!
if i % 9 == 0:
device.touch(x2, y, 'DOWN_AND_UP')
MonkeyRunner.sleep(pause)
# Swipe right
device.drag(start, end, duration, steps)
MonkeyRunner.sleep(pause)
# Swipe left
device.drag(end, start, duration, steps)
MonkeyRunner.sleep(pause)
瓦伦丁金莎的方法工作,如果你扩展你的观点,但如果您使用的是事件侦听器,使用:
view.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
Toast toast = Toast.makeText(
getApplicationContext(),
"View touched",
Toast.LENGTH_LONG
);
toast.show();
return true;
}
});
// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
// Dispatch touch event to view
view.dispatchTouchEvent(motionEvent);
更多关于获得MotionEvent对象,这里是一个优秀的回答:Android: How to create a MotionEvent?
当使用猴子脚本时,我注意到DispatchPress(KEYCODE_BACK) 没有做任何事情,真的很烂。在很多情况下,这是由于Activity没有使用Key事件。 此问题的解决方案是将猴脚本和 adb shell输入命令按顺序组合使用。
1使用猴子脚本给了一些伟大的时机 控制。等待一段时间的活动,并且是阻止adb呼叫的 。
2最后发送adb shell输入keyevent 4将结束正在运行的APK。
EG
ADB壳猴-p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
ADB壳输入的KeyEvent 4
使用ADB shell命令来模拟触摸事件
adb shell input tap x y
and also
adb shell sendevent /dev/input/event0 3 0 5
adb shell sendevent /dev/input/event0 3 1 29
- 1. 从android内核模拟触摸事件
- 2. 模拟iPhone中的触摸事件
- 3. 在后台线程中模拟Android中的触摸事件
- 4. 用javascript模拟触摸事件
- 5. BB10瀑布 - 模拟触摸事件
- 6. 上YUI模拟触摸事件3
- 7. 模拟触摸事件在一定间隔的Android
- 8. 如何模拟Windows 7中的触摸事件?
- 9. Android中的触摸事件
- 10. 在Android模拟器中模拟多点触摸
- 11. 如何延迟模拟的触摸事件?
- 12. 如何禁用触摸事件android
- 13. 在Windows 8中使用Python模拟触摸事件
- 14. 关于在Android中的触摸事件
- 15. 在Android中禁用WebView触摸事件
- 16. Android Soundpool - 触摸事件
- 17. Android TextView触摸事件
- 18. Android的触摸事件
- 19. 在iphone上模拟触摸
- 20. Android蓝牙触摸板模拟
- 21. Android模拟器支持Windows触摸屏
- 22. 触摸屏上的Windows将通过触摸手指来模拟MouseDown事件吗?
- 23. 在EditText上模拟触摸事件以显示软键盘
- 24. 在iOS上模拟系统宽触摸事件
- 25. 有没有办法模拟Windows 8中的触摸事件?
- 26. 是否有附加模块或扩展模拟触摸事件?
- 27. 触摸事件
- 28. 如何在android中停止触摸事件所需的时间?
- 29. 如何防止在Android中刷卡时发生触摸事件?
- 30. 如何在触摸事件android中添加移动图像?
您在下面得到了一些可行的答案,请记住,它们只会在可以进行小修改的应用程序上工作。对于其他无法修改的应用程序,您需要一个有根的平台来注入事件。 – 2010-12-09 08:20:24
是否可以让应用程序每10秒触摸x,y并将其最小化,但是在x,y中继续触摸? – 2014-11-22 16:40:57
检查我的答案没有根的要求。 :) – 2017-12-18 11:15:15