【问题标题】:android InputManager injectInputEventandroid InputManager 注入输入事件
【发布时间】:2013-09-09 13:42:34
【问题描述】:

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

【问题讨论】:

    标签: android input


    【解决方案1】:

    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();
    }
    

    【讨论】:

    • newEvent 传递什么?我得到错误预期 2 得到 1 与运动事件
    【解决方案2】:

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

    在这里寻找使用方法

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

    【讨论】:

      【解决方案3】:
      InputManager im = (InputManager) getSystemService(Context.INPUT_SERVICE);
      im.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
      

      【讨论】:

      • 试着解释你的答案。
      猜你喜欢
      • 2020-09-07
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 2015-02-14
      • 1970-01-01
      相关资源
      最近更新 更多