title: How to detect touch events outside the view in background service?
body: <p>I was trying to detect touch events in background service so that I can get all the touch events when user is using any apps. </p>
  
  <p>What's I've done is using WindowManager to add a small view and when running the app in the background, this small view can still be on the screen. I also set the view as an onTouchListener so when user touch inside the view, I can get the touch event.</p>
  
  <p>My problem is that is there any way to detect touch events outside this small view.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>public class GlobalTouchService extends Service implements View.OnTouchListener {
  
  
  private WindowManager mWindowManager;
  private WindowManager.LayoutParams mLayoutParams;
  private MyView myView;
  private boolean flag = true;
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      mWindowManager = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
      mLayoutParams = new WindowManager.LayoutParams();
      mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
      mLayoutParams.format = PixelFormat.TRANSLUCENT;
      mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
      mLayoutParams.x = 0;
      mLayoutParams.y = 0;
      mLayoutParams.height = 300;
      mLayoutParams.width = 300;
  
      myView = new MyView(this);
      myView.setOnTouchListener(this);
  
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      if (flag) {
          flag = false;
          mWindowManager.addView(myView, mLayoutParams);
      }
      return super.onStartCommand(intent, flags, startId);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  }
  
  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
      float x = motionEvent.getRawX();
      float y = motionEvent.getRawY();
      Log.d("x,y", "X" + x + " Y" + y);
  
      return false;
  }
  </code></pre>