如何在Android中模拟触摸事件?

时间:2022-03-10 20:49:02

How to simulate a touch event with Android while giving the X and Y coordinates manually?

如何用Android模拟一个触摸事件,同时手动给X和Y坐标?

7 个解决方案

#1


100  

Valentin Rocher's method works if you've extended your view, but if you're using an event listener, use this:

如果你扩展了你的视图,Valentin Rocher的方法是有效的,但是如果你使用的是事件监听器,请使用以下方法:

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);

For more on obtaining a MotionEvent object, here is an excellent answer: Android: How to create a MotionEvent?

关于获得MotionEvent对象的更多信息,这里有一个很好的答案:Android:如何创建一个MotionEvent?

#2


22  

Here is a monkeyrunner script that sends touch and drags to an application. I have been using this to test that my application can handle rapid repetitive swipe gestures.

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

#3


18  

use adb Shell Commands to simulate the touch event

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

#4


1  

If I understand clearly, you want to do this programatically. Then, you could use the onTouchEvent method of View, and create a MotionEvent with the coordinates you need.

如果我理解得很清楚,你想按程序来做。然后,您可以使用onTouchEvent View方法,并使用所需的坐标创建一个MotionEvent。

#5


1  

You should give the new monkeyrunner a go. Maybe this can solve your problems. You put keycodes in it for testing, maybe touch events are also possible.

你应该试一试新的《银翼杀手》。也许这能解决你的问题。你在里面放上密钥代码进行测试,也许触摸事件也是可能的。

#6


0  

When using Monkey Script I noticed that DispatchPress(KEYCODE_BACK) is doing nothing which really suck. In many cases this is due to the fact that the Activity doesn't consume the Key event. The solution to this problem is to use a mix of monkey script and adb shell input command in a sequence.

在使用Monkey脚本时,我注意到DispatchPress(KEYCODE_BACK)没有做任何非常糟糕的事情。在许多情况下,这是由于活动没有使用键事件。解决这个问题的方法是在序列中使用monkey脚本和adb shell输入命令的混合。

1 Using monkey script gave some great timing control. Wait a certain amount of second for the activity and is a blocking adb call.
2 Finally sending adb shell input keyevent 4 will end the running APK.

使用猴脚本可以很好的控制时间。等待一定的秒作为活动,这是一个阻塞的adb调用。最后发送adb shell输入keyevent 4将结束运行的APK。

EG

adb shell monkey -p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
adb shell input keyevent 4

亚行shell monkey -p com。应用-v -v -v -f /sdcard/monkey_script。txt 1 adb shell输入keyevent 4

#7


-6  

MotionEvent is generated only by touching the screen.

MotionEvent仅通过触摸屏幕生成。

#1


100  

Valentin Rocher's method works if you've extended your view, but if you're using an event listener, use this:

如果你扩展了你的视图,Valentin Rocher的方法是有效的,但是如果你使用的是事件监听器,请使用以下方法:

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);

For more on obtaining a MotionEvent object, here is an excellent answer: Android: How to create a MotionEvent?

关于获得MotionEvent对象的更多信息,这里有一个很好的答案:Android:如何创建一个MotionEvent?

#2


22  

Here is a monkeyrunner script that sends touch and drags to an application. I have been using this to test that my application can handle rapid repetitive swipe gestures.

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

#3


18  

use adb Shell Commands to simulate the touch event

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

#4


1  

If I understand clearly, you want to do this programatically. Then, you could use the onTouchEvent method of View, and create a MotionEvent with the coordinates you need.

如果我理解得很清楚,你想按程序来做。然后,您可以使用onTouchEvent View方法,并使用所需的坐标创建一个MotionEvent。

#5


1  

You should give the new monkeyrunner a go. Maybe this can solve your problems. You put keycodes in it for testing, maybe touch events are also possible.

你应该试一试新的《银翼杀手》。也许这能解决你的问题。你在里面放上密钥代码进行测试,也许触摸事件也是可能的。

#6


0  

When using Monkey Script I noticed that DispatchPress(KEYCODE_BACK) is doing nothing which really suck. In many cases this is due to the fact that the Activity doesn't consume the Key event. The solution to this problem is to use a mix of monkey script and adb shell input command in a sequence.

在使用Monkey脚本时,我注意到DispatchPress(KEYCODE_BACK)没有做任何非常糟糕的事情。在许多情况下,这是由于活动没有使用键事件。解决这个问题的方法是在序列中使用monkey脚本和adb shell输入命令的混合。

1 Using monkey script gave some great timing control. Wait a certain amount of second for the activity and is a blocking adb call.
2 Finally sending adb shell input keyevent 4 will end the running APK.

使用猴脚本可以很好的控制时间。等待一定的秒作为活动,这是一个阻塞的adb调用。最后发送adb shell输入keyevent 4将结束运行的APK。

EG

adb shell monkey -p com.my.application -v -v -v -f /sdcard/monkey_script.txt 1
adb shell input keyevent 4

亚行shell monkey -p com。应用-v -v -v -f /sdcard/monkey_script。txt 1 adb shell输入keyevent 4

#7


-6  

MotionEvent is generated only by touching the screen.

MotionEvent仅通过触摸屏幕生成。