14

I know how to "synthesize" a MotionEvent:

  event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);

What I am stuck at is how to "send/post/fire/distribute" it through the system, so that it is handled "as if" a real user actually touched the screen with his or her finger.

Is this possible at all?

If so, how do I accomplish this?

Regex Rookie
  • 10,432
  • 15
  • 54
  • 88

2 Answers2

19

What you are trying to do is perfectly possible and simple:

void simulateEventDown( View v, long x, long y )
{
    MotionEvent e = MotionEvent.obtain( SystemClock.uptimeMillis(),
                                        SystemClock.uptimeMillis(), 
                                        MotionEvent.ACTION_DOWN, 
                                        x, y, 0);
    v.dispatchTouchEvent(e);
}
pryma
  • 698
  • 8
  • 12
  • Thank you! This is exactly what I needed to get the right soft keyboard to open up in a WebView. I have to simulate a click into the input box. – Pre101 Feb 19 '12 at 08:08
  • @pryma all actions except ACION_MOVE are dispatching without proplem. when setting action to ACTION_MOVE it is always throwing arrayindexoutofboundsexception at MotionEvent.getX(). Do you have any idea what might be going on? – Gökhan Barış Aker May 15 '12 at 15:08
  • Looks like if `v` is a `ViewGroup` it won't dispatch event any further than it's `ViewGroup` object itself (in other words, it's equal to call `dispatchTouchEvent` and `onTouchEvent`). – Dmitry Zaytsev Nov 08 '12 at 12:12
  • calling `v.onTouchEvent(e)` instead of `v.dispatchTouchEvent(e)` resulted in different behaviors for me – Moh Mah May 12 '16 at 07:46
5

No, it's prevented by design. The concern is that such a feature can be used to subvert the entire security model - e.g. by "injecting" touches to contact the marketplace, arrange an install, accept the security warnings .. all on its own.

This has been discussed at some length here and following.

If this answers your question, kindly click the checkmark to the left - thanks!

DJC
  • 3,243
  • 3
  • 27
  • 31
  • If your answer turns out to be correct, then I don't like it. :) What you're saying makes sense but if you are familiar with the InstrumentationTestCase class I think it does what I am trying to accomplish. Are you totally sure about this? Is it possible to at least "fake" MotionEvents to **my** own app/activity/view only? – Regex Rookie Mar 09 '11 at 04:04
  • 1
    @REG If you turn out to be the one who is right, I'll like it :) The availability of InstrumentationTestCase, Monkey, etc indeed demonstrate the capacity for "add-on" event injectors. What I read (sorry, don't recall ref as this was months back) is that events come into the system through a hardware secured gate. If you have the key, you can inject events. The tools are apparently making use of special keys for use in highly restricted conditions. I look forward to hearing of anything else you may find... – DJC Mar 09 '11 at 20:33
  • I already gave you a +1 for the only clue so far I got about this challenge. I will give shortly another +1 for further clarifying this issue in your comment. As I said in my comment, I am not really interested in injecting an event into the **system**, I am only interested in accomplishing a text-selection task in one of **my** views. See http://stackoverflow.com/questions/5250290/why-doesnt-this-motionevent-simulation-work Is this possible at all? – Regex Rookie Mar 09 '11 at 21:09
  • for lack of an acceptable answer to http://stackoverflow.com/questions/5250290/why-doesnt-this-motionevent-simulation-work I concede defeat and accept your answer. :( – Regex Rookie Mar 18 '11 at 18:47