【问题标题】:Inject motion event via windowmanger?通过窗口管理器注入运动事件?
【发布时间】:2012-05-04 06:08:31
【问题描述】:

我想模拟触摸事件。我正在使用此代码

MotionEvent.PointerCoords[] coords = { new MotionEvent.PointerCoords() };
coords[0].x = 200;
coords[0].y = 200;
int[] ptrs = { 0 };
MotionEvent event =MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(), action, 1, ptrs, coords, 0, 1, 1, 0, 0,    InputDevice.SOURCE_TOUCHPAD, 0);
windowManager.injectPointerEvent(event, false);

问题出在这一行

windowManager.injectPointerEvent(event, false);

我无法访问 WindowManger 。当我尝试使用它时

 WindowManager windowmanager=(WindowManager) Context.getSystemService(Context.WINDOW_SERVICE);

我收到一条错误消息。 “无法从类型 Context 对非静态方法 getSystemService(String) 进行静态引用”

谁能帮帮我!!

【问题讨论】:

  • 基本上你只能在你的应用中实现这个目标,或者在有根设备上

标签: android events touch


【解决方案1】:

这个错误:I received an error message . "Cannot make a static reference to the non-static method getSystemService(String) from the type Context" 是你的问题。

您需要获取对上下文的引用。应用程序上下文或活动。

一旦你有了一个 Context 实例,你就可以在它上面调用.getSystemService 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2019-08-16
    相关资源
    最近更新 更多