2010-12-09 186 views
85

如何使用Android模拟触摸事件,同时手动输入X和Y坐标?如何在Android中模拟触摸事件?

+1

您在下面得到了一些可行的答案,请记住,它们只会在可以进行小修改的应用程序上工作。对于其他无法修改的应用程序,您需要一个有根的平台来注入事件。 – 2010-12-09 08:20:24

+0

是否可以让应用程序每10秒触摸x,y并将其最小化,但是在x,y中继续触摸? – 2014-11-22 16:40:57

+0

检查我的答案没有根的要求。 :) – 2017-12-18 11:15:15

回答

1

你应该给新的monkeyrunner一个去。也许这可以解决你的问题。你把键码放在里面进行测试,也许触摸事件也是可能的。

+1

请让我知道如何安装monkeyrunner。 adb不识别monkeyrunner – indira 2010-12-10 04:24:08

+0

这与adb的ui excersiser猴子无关。你会在adt的版本9的工具目录中找到monkeyrunner。 – keyboardsurfer 2010-12-10 11:38:05

+0

同时查看我在答案中提供的链接。这导致Google提供的有关monkeyrunner的信息。 – keyboardsurfer 2010-12-10 12:26:51

1

如果我理解清楚,你想以编程方式做到这一点。然后,您可以使用方法View,并用您需要的坐标创建MotionEvent

-6

MotionEvent仅通过触摸屏幕生成。

22

这是一个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) 
97

瓦伦丁金莎的方法工作,如果你扩展你的观点,但如果您使用的是事件侦听器,使用:

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?

0

当使用猴子脚本时,我注意到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

17

使用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