android InputManager injectInputEvent

时间:2013-09-09 13:42:34

标签: android input

我看过this。我无法编译coredump给出的答案。我可以清楚地看到InputManager.java中的injectInputEvent(Android源代码)。它也是公开的。但是我无法编译它。可能是它的私人api,并且有办法访问它..

3 个答案:

答案 0 :(得分:6)

隐藏了API。您可以通过反射

访问它
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 :(得分:1)

这是一个隐藏的API(@hide)

在这里查看使用它的方法

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

答案 2 :(得分:-1)

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