0

I am creating android activity that starts a service. This service is intended to receive touch events even if user is using other application. Its onCreate() method is as follow. public void onCreate() {

    super.onCreate(); 
    // create linear layout
    touchLayout = new LinearLayout(this);
    // set layout width 30 px and height is equal to full screen
    LayoutParams lp = new LayoutParams(30, LayoutParams.MATCH_PARENT);
    touchLayout.setLayoutParams(lp);
    // set color if you want layout visible on screen
    //touchLayout.setBackgroundColor(Color.CYAN); 
    // set on touch listener
    touchLayout.setOnTouchListener(this);

    // fetch window manager object 
     mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
     // set layout parameter of window manager
     WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
                //30, // width of layout 30 px
             WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
                WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls).
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  , // this window won't ever get key input focus  
                PixelFormat.TRANSLUCENT);      
     mParams.gravity = Gravity.LEFT | Gravity.TOP;   
    Log.i(TAG, "add View");

     mWindowManager.addView(touchLayout, mParams);

}

Above i am creating a window that span full height and width of screen. I have set it to listen to touch events. But doing so stops other applications receive touch event. So i am looking to dispatch these touch events received on my service to background application on which my window is placed.

Please Help !

1 Answers1

0

Sandip,

Adding an overlay view to the window manager automatically places that view at the top of that window's view hierarchy, meaning that it will intercept touches instead of the view(s) behind it. The only way for views behind it to receive touch events is for the user to touch outside of the top view (which is impossible since it spans the entire screen), or for the top view to label itself "untouchable".

So, either restrict the size of your view, or add the flag WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE to your params flags.

In either case, UNLESS the user currently has your app in the foreground, any touch events outside of your view will return an ACTION_OUTSIDE touch event, but without any coordinates on the location of the touch. (If the user does have your app in the foreground, then you'll receive coordinates with your ACTION_OUTSIDE events.)

Halogen
  • 551
  • 6
  • 11