Reputation: 41
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
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
Reputation: 1219
There is a tool that comes with the SDK called Monkey which generates pseudo-random streams of user events such as:
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