android InputManager injectInputEvent

Jai*_*ken 3 android input

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

Dav*_*and 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();
}
Run Code Online (Sandbox Code Playgroud)