TorukMakto
TorukMakto

Reputation: 2088

android InputManager injectInputEvent

I have read this. I am not able to compile the answer given by coredump. I can clearly see the injectInputEvent in InputManager.java (Android source code). Its public too. However I am not able to compile it. May be its a private api and there's a way to access it..

Upvotes: 4

Views: 8569

Answers (3)

wangxin
wangxin

Reputation: 29

InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
im.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);

Upvotes: -2

David Ferrand
David Ferrand

Reputation: 5470

The API is hidden. You can access it by reflection:

InputManager im = (InputManager) getSystemService(Context. INPUT_SERVICE);

Class[] paramTypes = new Class[2];
paramTypes[0] = InputEvent.class;
paramTypes[1] = Integer.TYPE;

Object[] params = new Object[2];
params[0] = newEvent;
params[1] = 0;

try {
    Method hiddenMethod = im.getClass().getMethod("injectInputEvent", paramTypes);
    hiddenMethod.invoke(im, params);
} catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    e.printStackTrace();
}

Upvotes: 7

Arkady
Arkady

Reputation: 664

it's a hidden API (@hide)

look here for a way to use it

http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/

Upvotes: 1

Related Questions