如何服务侦听触摸手势/事件?手势、事件

2023-09-12 03:53:35 作者:我喂自己袋盐

我想知道像SwipePad和波发射器的应用程序是如何能够简单地通过服务来检测触摸手势/事件。这些应用程序能够检测到触摸手势,即使它是不是在自己的活动。我找遍了整个互联网,并没有发现他们怎么能做到这一点。

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 even though it is not in their own Activity. I've looked all over the Internet and haven't found how they can do that.

我的主要问题是如何服务可以在触摸guestures监听/只是作为一个经常性活动的事件可能会收到MotionEvents即使它可能不会在原来的活动或上下文。我基本上是试图构建一个应用程序,将recongize来自用户的特定触摸手势不管是哪个活动是在上面,做一些事情时手势recongized。触摸recongition将在后台服务运行的线程。

My main question is how a service can listen in on touch guestures/events just as a regular Activity may receive MotionEvents even though it may not be in the original Activity or context. I'm essentially trying a build an app that will recongize a particular touch gesture from a user regardless which Activity is on top and do something when that gesture is recongized. The touch recongition will be a thread running in the background as a service.

推荐答案

我有这个同样的问题,我终于理解了它!多亏了这个帖子:creating系统覆盖(总在最前)在Android中按钮。您需要使用的,而不是叠加一个警告窗口(这也意味着你可以使用它在Andoid ICS):

I had this same problem and I've finally figured it out! Thanks to this post: creating a system overlay (always on top) button in android. 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);

然后,只需附上GestureListener以这种方式:

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

耶!