本文介绍了一个TYPE_SYSTEM_OVERLAY内GestureDetector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 TYPE_SYSTEM_OVERLAY ,我希望捕捉到手指/使用服务触及这个叠加的事件。

该覆盖工作正常,但问题是它不能捕捉触摸事件,当我尝试纳入 GestureDetector 在里面。当我点击屏幕上,面包味精onDown没有显示..

将AP preciate如果有人告诉我,我在哪里出了错:(我已经试过落实 GestureDetector 之外OverlayView的类,但也没有任何结果。

 公共类NUSLogService扩展IntentService {
      OverlayView的MView的;      @覆盖
       公共无效的onCreate(){
          super.onCreate();          最后WindowManager.LayoutParams PARAMS =新WindowManager.LayoutParams(
              WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
              WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
              PixelFormat.TRANSLUCENT);              params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
              params.setTitle(TouchLayer);
              MVIEW =新OverlayView的(getApplicationContext());          最后的窗口管理器,WM =(窗口管理器)getSystemService(WINDOW_SERVICE);
              wm.addView(MVIEW,则params);
      }
     @覆盖
     公共无效的onDestroy(){
          super.onDestroy();
          如果(MVIEW!= NULL)
          {
            ((窗口管理器)getSystemService(WINDOW_SERVICE))removeView(MVIEW)。
            MVIEW = NULL;
          }
      }    //自取覆盖类
    类OverlayView的扩展的ViewGroup实现OnGestureListener {      私人GestureDetector gestureScanner =新GestureDetector(本);      公共OverlayView的(上下文的背景下){
         超级(上下文);
         Toast.makeText(的getContext(),OverlayView的,Toast.LENGTH_SHORT).show();
      }      @覆盖
       公共布尔onTouchEvent(MotionEvent事件){
  //返回super.onTouchEvent(事件);
           返回gestureScanner.onTouchEvent(事件);
      }      @覆盖
      保护无效onLayout(布尔为arg0,ARG1 INT,INT ARG2,诠释ARG3,诠释ARG4){
      // TODO自动生成方法存根
      }      @覆盖
      公共布尔onDown(MotionEvent五){
         Toast.makeText(的getContext(),onDown,Toast.LENGTH_SHORT).show();
         返回false;
      }
}


解决方案

首先,这不应该是一个 IntentService 。在当前的实施,的onDestroy()将在的onCreate称为一两毫秒()

二, TYPE_SYSTEM_OVERLAY 不能接收任何触摸事件的Andr​​oid 4.0,出于安全原因。

I have created a TYPE_SYSTEM_OVERLAY and i wish to capture finger/touch events on this overlay using a service.

The overlay works fine, but the problem is it cannot capture touch events when i try to incorporate the GestureDetector in it. When i tap on screen, the toast msg "onDown" didnt show..

will appreciate if anyone tells me where have i went wrong :( I have tried implement the GestureDetector outside of the OverlayView class but also no result.

public class NUSLogService extends IntentService {
      OverlayView mView;

      @Override
       public void onCreate() {
          super.onCreate();

          final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
              WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
              PixelFormat.TRANSLUCENT);

              params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
              params.setTitle("TouchLayer");
              mView = new OverlayView(getApplicationContext());

          final WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
              wm.addView(mView, params);
      }
     @Override
     public void onDestroy() {
          super.onDestroy();
          if(mView  != null)
          {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView );
            mView  = null;
          }
      }

    //self-created overlay class
    class OverlayView extends ViewGroup implements OnGestureListener{

      private GestureDetector gestureScanner = new GestureDetector(this);

      public OverlayView(Context context) {
         super(context);
         Toast.makeText(getContext(),"OverlayView", Toast.LENGTH_SHORT).show();
      }

      @Override
       public boolean onTouchEvent(MotionEvent event) {
  //        return super.onTouchEvent(event);
           return gestureScanner.onTouchEvent(event);
      }

      @Override
      protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
      // TODO Auto-generated method stub
      }

      @Override
      public boolean onDown(MotionEvent e) {
         Toast.makeText(getContext(),"onDown", Toast.LENGTH_SHORT).show();
         return false;
      }
}
解决方案

First, this should not be an IntentService. In your current implementation, onDestroy() will be called a millisecond or two after onCreate().

Second, TYPE_SYSTEM_OVERLAY cannot receive any touch events as of Android 4.0, for security reasons.

这篇关于一个TYPE_SYSTEM_OVERLAY内GestureDetector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:43