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?
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.
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.
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.
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.
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);
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With