4

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

4

3 回答 3

7

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();
}
于 2014-07-07T10:07:32.463 回答
1

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

在这里寻找使用它的方法

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

于 2013-10-21T14:23:37.640 回答
-2
InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
im.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
于 2016-06-13T08:10:23.753 回答