Simulate touch screen events on Android

Is it possible for the background process or soft keyboard to create touch events and send them to the screen as if the screen was really affected?

i.e. simulating touch screen events.

+4
source share
2 answers

There is a tool that comes with an SDK called Monkey that generates pseudo-random streams of custom events, for example:

  • clicks
  • strokes
  • gestures
  • a series of events at the system level.

You can use the monkey for the stress testing applications you develop in a random but repeatable manner.

There is also a monkeyrunner tool that provides an API for writing programs that control an Android device or emulator from outside Android code. With monkeyrunner, you can write a Python program that installs an Android application or test suite, launches it, sends keystrokes to it, takes screenshots of its user interface and saves screenshots on the workstation.

+3
source

To press the buttons, you can use the following:

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); 

The list of available key codes can be found here: http://developer.android.com/reference/android/view/KeyEvent.html

For touchscreen events, you can use:

  dispatchTouchEvent(MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, Xinput, Yinput, 0)); 
+2
source

All Articles