Android inject touch event
Asked Answered
G

2

7

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!

Gladine answered 10/9, 2013 at 5:38 Comment(0)
G
1

For the benefit of larger group - rootViewController.dispatchTouchEvent (instead of activity.rootViewController) works like a charm.

Gladine answered 10/9, 2013 at 12:51 Comment(1)
How can rootViewController be obtained?Margaritamargarite
M
5
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.

Madalena answered 10/9, 2013 at 17:33 Comment(0)
G
1

For the benefit of larger group - rootViewController.dispatchTouchEvent (instead of activity.rootViewController) works like a charm.

Gladine answered 10/9, 2013 at 12:51 Comment(1)
How can rootViewController be obtained?Margaritamargarite

© 2022 - 2024 — McMap. All rights reserved.