Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android detect or block floating/overlaying apps

Tags:

android

Android enables apps to draw over other apps with android.permission.SYSTEM_ALERT_WINDOW and it's called a floating/overlaying app. For example Facebook Messenger has always visible chat bubbles at the edges of screen.

My question is: Is it possible to detect or block in Java code any app which draws over my app?

like image 654
jrwm Avatar asked Sep 09 '25 21:09

jrwm


1 Answers

There is a View#onFilterTouchEventForSecurity() method you can override to detect if the motion event has the FLAG_WINDOW_IS_OBSCURED. This will let you know if something is drawn on top of your view.

@Override
public boolean onFilterTouchEventForSecurity(MotionEvent event) {
    if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) == MotionEvent.FLAG_WINDOW_IS_OBSCURED){
        // show error message
        return false;
    }
    return super.onFilterTouchEventForSecurity(event);
}

If you just want to protect your app from tap jacking due to another app drawing over your app you can add setFilterTouchesWhenObscured to your views via XML or programmatically.

like image 178
Don Cung Avatar answered Sep 12 '25 12:09

Don Cung