Use programming to set touch input on android

霸气de小男生 提交于 2019-12-02 04:36:26

问题


I want to use programming to mimic a "touch" on a position (x, y) on the screen.

I saw this link, but it only teaches you how to inject characters.

I want to inject a touch at position (x, y)

How do I do this using Android SDK (which is java)?


回答1:


You can "fake" a touch event like this:

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, metaState);
view.onTouchEvent(event);

API Docs:

Create a new MotionEvent, filling in a subset of the basic motion values. Those not specified here are: device id (always 0), pressure and size (always 1), x and y precision (always 1), and edgeFlags (always 0).

Parameters:

  • downTime The time (in ms) when the user originally pressed down to start a stream of position events. This must be obtained from SystemClock.uptimeMillis().
  • eventTime The the time (in ms) when this specific event was generated. This must be obtained from SystemClock.uptimeMillis().
  • action The kind of action being performed -- one of either ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL.
  • x The X coordinate of this event.
  • y The Y coordinate of this event.
  • metaState The state of any meta / modifier keys that were in effect when the event was generated.

Link to API Docs




回答2:


That would be Monkeyrunner to do the job



来源:https://stackoverflow.com/questions/30245179/use-programming-to-set-touch-input-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!