How can a service listen for touch gestures/events?

前端 未结 5 1739
猫巷女王i
猫巷女王i 2020-11-29 16:29

I\'m wondering how apps like SwipePad and Wave Launcher are able to detect touch gestures/events simply through a service. These apps are able to detect a touch gestures eve

5条回答
  •  忘掉有多难
    2020-11-29 17:06

    I had this same problem and I've finally figured it out! Thanks to this post: Creating a system overlay window (always on top). You need to use an alert window instead of an overlay (and this also means you can use it in Andoid ICS):

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);
    

    Then just attach a GestureListener in this manner:

    GestureDetector gestureDetector = new GestureDetector(this, new AwesomeGestureListener());
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
          public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
          }
    };
    
    overlayView.setOnTouchListener(gestureListener);
    

    Yay!

提交回复
热议问题