user993901
user993901

Reputation: 41

Simulating Touch Screen Events on Android

Is it possible for a background process or softkeyboard to create touch events and send them to screen as if the screen was actually touched?

i.e. simulating touch screen events.

Upvotes: 4

Views: 4559

Answers (2)

keag
keag

Reputation: 299

For button presses, you can use the following:

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

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

A list of available keycodes 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));

Upvotes: 2

silleknarf
silleknarf

Reputation: 1219

There is a tool that comes with the SDK called Monkey which generates pseudo-random streams of user events such as:

  • clicks
  • touches
  • gestures
  • a number of system-level events.

You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.

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

Upvotes: 3

Related Questions