2

我创建了一个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;
      }
}
4

1 回答 1

2

首先,这不应该是一个IntentService. 在您当前的实现中,onDestroy()将在一两毫秒后调用onCreate().

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

于 2012-03-15T11:41:37.237 回答