Android is it possible to create a service overlay and intercept all touch event data?

476 views Asked by At

Hello I am trying to create a transparent overlay that intercepts ( and does not block the other applications) all the touch events I do on my smartphone, particularly I'm interested in the scroll event, this is the code from my service class:

  @Override
public void onCreate() {
    super.onCreate();
    windowManager=(WindowManager)this.getSystemService(Context.WINDOW_SERVICE);
    view=new LinearLayoutCompat(this);


    LayoutParams params=new LayoutParams(1, ViewGroup.LayoutParams.MATCH_PARENT);


    view.setLayoutParams(params);
    view.setOnTouchListener(this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    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;
    windowManager.addView(view, params);
    return super.onStartCommand(intent, flags, startId);
}




@Override
public boolean onTouch(View view, MotionEvent motionEvent) {


    Log.d(TAG,motionEvent.toString());



    return false;
}

However with this I only get ACTION_OUTSIDE type of motion event, and I need all the array of events for me to keep track of scrolling, thank you in advance !

1

There are 1 answers

1
CommonsWare On BEST ANSWER

I am trying to create a transparent overlay that intercepts ( and does not block the other applications) all the touch events I do on my smartphone

Fortunately, this is not possible, for obvious privacy and security reasons, on Android 4.0+. Malware authors love this sort of "tapjacking" attack, invisibly spying on user input.