服务如何听取触摸手势/事件? [英] How can a service listen for touch gestures/events?

查看:169
本文介绍了服务如何听取触摸手势/事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道像SwipePad和Wave Launcher这样的应用程序是否能够通过服务来检测触摸手势/事件。这些应用程序能够检测到触摸手势,即使它们不在自己的活动中。我已经浏览了整个互联网,还没有找到如何做到这一点。



我的主要问题是服务可以如何在接触客户/活动中聆听,正如一个常规的活动可能会收到MotionEvents,尽管它可能不在原始的活动或上下文。我基本上试图构建一个应用程序,该应用程序将从用户处获取特定的触摸手势,无论哪个Activity处于顶部,并在该手势被通知时执行某些操作。 touch /弄清楚了!感谢这篇文章:创建系统覆盖窗口(始终为在顶部)。您需要使用警报窗口而不是叠加层(这也意味着您可以在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:

  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!


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.

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.

解决方案

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!

这篇关于服务如何听取触摸手势/事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆