4

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..

3 Answers 3

7

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();
}
1
  • what to pass in newEvent? I got error expected 2 got 1 with motion event Nov 11, 2019 at 9:41
1

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/

-2
InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
im.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
1
  • Try to explain your answer. Jun 13, 2016 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.