【发布时间】:2013-09-09 13:42:34
【问题描述】:
我已阅读this。我无法编译 coredump 给出的答案。我可以清楚地看到 InputManager.java(Android 源代码)中的 injectInputEvent。它也是公众的。但是我无法编译它。可能是它的私有 api,并且有一种方法可以访问它..
【问题讨论】:
我已阅读this。我无法编译 coredump 给出的答案。我可以清楚地看到 InputManager.java(Android 源代码)中的 injectInputEvent。它也是公众的。但是我无法编译它。可能是它的私有 api,并且有一种方法可以访问它..
【问题讨论】:
API 已隐藏。您可以通过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();
}
【讨论】:
这是一个隐藏的 API (@hide)
在这里寻找使用方法
http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/
【讨论】:
InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
im.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
【讨论】: