我创建了一个TYPE_SYSTEM_OVERLAY,并且希望使用service捕获此覆盖层上的手指/触摸事件。

覆盖效果很好,但是问题是当我尝试在其中加入GestureDetector时它无法捕获触摸事件。当我点击屏幕时,吐司味精“ onDown”没有显示。

如果有人告诉我我哪里出错了,将不胜感激:(我尝试在GestureDetector类之外实现OverlayView,但也没有结果。

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

最佳答案

首先,这不应是IntentService。在您当前的实现中,onDestroy()将在onCreate()之后被称为一两毫秒。

其次,出于安全原因,TYPE_SYSTEM_OVERLAY从Android 4.0开始无法接收任何触摸事件。

关于java - TYPE_SYSTEM_OVERLAY中的GestureDetector,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9717763/

10-08 23:27