Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android inject touch event

I know this is a bit repeating question. I have reviewed various questions on SO and few other sites but could not find a single answer that satisfies my need.

Requirement

1) I have X,Y coordinates, I want to create MotionEvent and dispatch it to topmost activity/view.

2) I have to inject it in my own App. So Inject_Events permission should not be required.

3) While I have the coordinates, I do not know, Activity or View which are at that position. So Activity.dispatchTouchEvent does not work for me.. (view.dispatchTouchEvent will work but I do not know the view).

4) Instrumentation does not help either because they do not have single method to inject touch event at x,y location.

5) InputManager has hidden method injectInputEvent but I don't know how to access injectInputEvent and compile it?

Any help appreciated!

like image 649
TorukMakto Avatar asked Sep 10 '13 05:09

TorukMakto


People also ask

How do you simulate a touch event on android?

Valentin Rocher's method works if you've extended your view, but if you're using an event listener, use this: view. setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Toast toast = Toast. makeText( getApplicationContext(), "View touched", Toast.

How does touch event work android?

Touch events are delivered first to Activity. dispatchTouchEvent. It's where you may catch them first. Here they get dispatched to Window, where they traverse View hierarchy, in such order that Widgets that are drawn last (on top of other widgets) have chance to process touch in View.

How do you implement touch events?

Try code below to detect touch events. mView. setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //show dialog here return false; } }); To show dialog use Activity method showDialog(int).

How do I turn off touch events on Android?

Search for Enable resampling input events. Click on Enable touch events > Disabled.


1 Answers

dispatchTouchEvent(MotionEvent.obtain(
                    SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN, Xinput, Yinput, 0));

You do not need to specify a view, the event will register to the foreground activity. Replace Xinput and Yinput with your values.

like image 156
keag Avatar answered Sep 25 '22 09:09

keag