Note that there are some explanatory texts on larger screens.

plurals
  1. POdispatching touch events from overlay view to background
    primarykey
    data
    text
    <p>I have the following service to display an overlay view over every app in the system and getting the touch events. Because of the TYPE_SYSTEM_ALERT the touches can't go through to the background so it's useless. I thought that I could obtaing a MotionEvent and dispatch it to the background view using the same x,y that I just detected. </p> <p>**QUESTION: How do I define the background view to which I will send the event?. Given that I can detect at all times what the foreground application is, can I define a view of that window so that i can dispatch the event?</p> <pre><code>public class OverlayService extends Service { LinearLayout oView; @Override public IBinder onBind(Intent i) { return null; } @Override public void onCreate() { super.onCreate(); oView = new LinearLayout(this); oView.setBackgroundColor(0x88ff0000); // The translucent red color WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(oView, params); oView.setOnTouchListener(new View.OnTouchListener() { @SuppressLint("Recycle") @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()){ case MotionEvent.ACTION_DOWN: int x = (int) event.getX(); int y = (int) event.getY(); long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + 100; // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState ); Toast.makeText(getBaseContext(), Integer.toString(x)+" "+Integer.toString(y), Toast.LENGTH_SHORT).show(); // Dispatch touch event to background // ? // ? // Dispatch touch event to background } // Obtain MotionEvent object return false; } }); } @Override public void onDestroy() { super.onDestroy(); if(oView!=null){ WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.removeView(oView); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload