Detecting Android screen overlays programmatically

前端 未结 1 1163
梦谈多话
梦谈多话 2021-02-08 14:00

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

相关标签:
1条回答
  • 2021-02-08 14:51

    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.

    0 讨论(0)
提交回复
热议问题