How to simulate a Touch event in Android?

How to simulate a touch event with Android, while specifying the X and Y coordinates?

+91
android adb gesture-recognition
Dec 09 '10 at 7:59
source share
7 answers

The Valentin Rocher method works if you have expanded your view, but if you use an event listener, use this:

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

More on getting a MotionEvent object, here is a great answer: Android: How to create a MotionEvent?

+102
Aug 09 '11 at 18:52
source share

Here is a monkey gatekeeper script that sends a touch and drags the application. I used this to verify that my application can handle fast repeating gestures.

 # 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) 
+22
Jan 19 2018-11-11T00:
source share

use adb shell commands to simulate touch events

 adb shell input tap xy and also adb shell sendevent /dev/input/event0 3 0 5 adb shell sendevent /dev/input/event0 3 1 29 
+19
Mar 12 '14 at 12:39
source share

You have to give a new monkeyrunner a go. Perhaps this may solve your problems. You put key codes into it for testing, perhaps touch events are also possible.

+1
Dec 09 '10 at 8:10
source share

If I understand clearly, you want to do this programmatically. Then you can use the onTouchEvent View method and create a MotionEvent with the necessary coordinates.

+1
Dec 09 '10 at 8:11
source share

When using Monkey Script, I noticed that DispatchPress (KEYCODE_BACK) does nothing that really sucks. In many cases, this is because Activity does not use the Key event. The solution to this problem is to use a mixture of Monkey Script and the adb command input command in sequence.

1 Using Monkey Script gave an excellent timing control. Wait a few seconds for activity and block adb call.
2 Finally, sending the input key of the 4th adb key will end the APK.

E.G.

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

0
Jan 25 '13 at 4:25
source share

MotionEvent is generated only by touching the screen.

-6
Dec 10 '10 at 4:18
source share



All Articles