如何在我的服务中捕获其他应用程序的用户触摸事件?

时间:2019-06-28 10:40:59

标签: android overlay detect motionevent

我正在尝试在我的服务中记录所有触摸事件。 我找到了一些解释捕获触摸事件的代码。 但是这些代码不能解决我的问题。 我的问题是关注者。

如何在我的服务中捕获其他应用程序的用户触摸事件?(例如游戏)

@Override
    protected void onServiceConnected() {
        // Create an overlay and display the action bar
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mLayout = new FrameLayout(this);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(1, WindowManager.LayoutParams.MATCH_PARENT);
        mLayout.setLayoutParams(params);
        mLayout.setOnTouchListener(this);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
        lp.format = PixelFormat.TRANSPARENT;
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        lp.width = 1;
        lp.height = 1;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        wm.addView(mLayout, lp);

    }

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {        
         Log.d(TAG, "Touch event: " + motionEvent.toString());
        return false;
    }

运行上面的代码时,我获得了以下结果。

2019-06-28 19:16:02.477 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19955497, downTime=19955497, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:03.021 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19956041, downTime=19956041, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:03.589 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19956609, downTime=19956609, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:04.356 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19957377, downTime=19957377, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:04.828 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19957849, downTime=19957849, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:05.228 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19958248, downTime=19958248, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:05.580 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19958600, downTime=19958600, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:06.333 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19959352, downTime=19959352, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:06.973 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19959993, downTime=19959993, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:08.005 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19961024, downTime=19961024, deviceId=0, source=0x1002, displayId=0 }
2019-06-28 19:16:08.469 24827-24827/com.example.android.demoservice D/Nabi_Touch: Touch event: MotionEvent { action=ACTION_OUTSIDE, actionButton=0, id[0]=0, x[0]=0.0, y[0]=0.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=19961488, downTime=19961488, deviceId=0, source=0x1002, displayId=0 }

您会看到指针位置为x[0]=0.0, y[0]=0.0

如何捕获正确的指针位置?

0 个答案:

没有答案