开发者

Simulating Touch Screen Events on Android

开发者 https://www.devze.com 2023-04-13 08:00 出处:网络
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?

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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号