Is there a legal way in Android to listen for ALL the touch events user makes, even outside my app. I mean to have something like a service that listens for screen gestures in a background.
-
You should get your answer from this post http://stackoverflow.com/questions/6714020/how-can-a-service-listen-for-touch-gestures-events – Abdullah May 25 '16 at 09:27
-
@GodslaveAsad Well I've seen that post but they only provide a solution that interfere with the user's activity on the screen, and I want to stay completely unseen. – Salivan May 25 '16 at 09:30
1 Answers
The idea is to define a dummy UI fragment that is really tiny (say, 1x1 pixel), and place it on one of the corners of the screen, and let it listen on all touch events outside it. Well, literally, it's not "invisible", in fact it's in foreground all the time! But since it's so tiny so hopefully users won't feel a difference.
First, let's create this dummy view:
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
mDummyView = new LinearLayout(mContext);
LayoutParams params = new LayoutParams(1, LayoutParams.MATCH_PARENT);
mDummyView.setLayoutParams(params);
mDummyView.setOnTouchListener(this);
Here we set the width of the dummy view to be 1 pixel, and the height to be parent height. And we also set up a touch event listen of this dummy view, which we'll implement later.
Then let's add this dummy view.
LayoutParams params = new LayoutParams(
1, /* width */
1, /* height */
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE |
LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSPARENT
);
params.gravity = Gravity.LEFT | Gravity.TOP;
mWindowManager.addView(mDummyView, params);
The key here is the FLAG_WATCH_OUTSIDE_TOUCH flag, it enables the dummy view to capture all events on screen, whether or not the event is inside the dummy view or not.
Finally, let's handle the touch event by implementing View.OnTouchListener listener.
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "Touch event: " + event.toString());
// log it
return false;
}
We need to return false since we're not really handling the event, so that the underlying real UI elements can get those events.
A final note is that, to keep our dummy view always listening touch events, we need to wrap all these in an Service: we create the dummy view in onCreate and add it to screen in onStartCommand. And the service should implement View.OnTouchListener to receive the touch events.
Referrence I got this from here http://jhshi.me/2014/11/09/monitor-screen-touch-event-in-android/

- 935
- 9
- 18
-
Nice answer, though, but I guess it would show me wrong coordinates if events are captured outside that dummy view, and I need to know where exactly the touch happened. – Salivan May 25 '16 at 09:41
-
This approach will not work, because events that are outside the current context will always return 0. This is a change from Android 4.3 and above (source: https://github.com/android/platform_frameworks_base/blob/79e0206ef3203a1842949242e58fa8f3c25eb129/services/input/InputDispatcher.cpp#L1417) – sparkonhdfs Jun 09 '16 at 21:15
-
This code has some permission problem Unable to add window android.view.ViewRootImpl$W@3daf7d3 -- permission denied – SWIK Jul 04 '21 at 02:28