服务如何监听触摸手势/事件? [英] How can a service listen for touch gestures/events?
问题描述
我想知道像 SwipePad 和 Wave Launcher 这样的应用程序如何能够仅通过服务检测触摸手势/事件.这些应用程序能够检测到触摸手势,即使它不在它们自己的 Activity 中.我已经在互联网上查看了所有内容,但还没有找到他们如何做到这一点.
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.
我的主要问题是服务如何监听触摸事件/事件,就像常规 Activity 可能接收 MotionEvents 一样,即使它可能不在原始 Activity 或上下文中.我基本上是在尝试构建一个应用程序,该应用程序将识别来自用户的特定触摸手势,无论哪个 Activity 位于顶部,并在识别该手势时执行某些操作.触摸识别将是一个作为服务在后台运行的线程.
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.
推荐答案
我遇到了同样的问题,我终于解决了!感谢这篇文章:创建系统覆盖窗口(总是在顶部).您需要使用警报窗口而不是覆盖层(这也意味着您可以在 Andoid ICS 中使用它):
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);
然后以这种方式附加一个 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);
耶!
这篇关于服务如何监听触摸手势/事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!