GestureDetector within a TYPE_SYSTEM_OVERLAY

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 10:36:46

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/9717763/gesturedetector-within-a-type-system-overlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!