Detecting Android screen overlays programmatically

▼魔方 西西 提交于 2019-12-09 10:37:58

问题


Is there a way for an app to:

  1. check if there exists screen overlay(s) on top of it, and
  2. figure out what package name owns the overlay(s)?

I know Android M and above is able to detect screen overlays when in the permissions page and deny permission changes whenever it detects screen overlays, but are developers able to achieve the same things in the app layer?


回答1:


You can detect overlays by checking for the MotionEvent.FLAG_WINDOW_IS_OBSCURED flag when the user touches one of your Views.

final View.OnTouchListener filterTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Filter obscured touches by consuming them.
        if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                Toast.makeText(v.getContext(), "Overlay detected", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return false;
    }
};

yourButton.setOnTouchListener(filterTouchListener);

Source: Android M's settings app

However, I don't think it's possible to detect which app owns the overlay.



来源:https://stackoverflow.com/questions/44197671/detecting-android-screen-overlays-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!