Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send synthesized MotionEvent through the system?

Tags:

android

events

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?

like image 559
Regex Rookie Avatar asked Mar 09 '11 01:03

Regex Rookie


People also ask

How do you make a motion event on android?

Use the getPointerId(int) method to obtain a pointer id to track pointers across motion events in a gesture. Then for successive motion events, use the findPointerIndex(int) method to obtain the pointer index for a given pointer id in that motion event.

When tracking touch what does Action_up represent?

In theory, if you get an ACTION_DOWN event and then an ACTION_UP event - it means that the user has just clicked your View.

What is Action_up and Action_down?

It doesn't mean a gesture like swipe down it means that user touched the screen (finger down, the touch or gesture just started). Now you need to catch MotionEvent.ACTION_UP (finger up, gesture or touch ends here) and decide if there was a gesture you need. http://developer.android.com/training/gestures/detector.html.

What is a motion event?

Motion Event Explanation Motion events describe movements in terms of an action code and a set of axis values. The action code specifies the state change that occurred such as a pointer going down or up. The axis values describe the position and other movement properties.


2 Answers

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);
}
like image 70
pryma Avatar answered Sep 22 '22 19:09

pryma


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!

like image 21
DJC Avatar answered Sep 25 '22 19:09

DJC