以编程方式检测Android屏幕叠加层

时间:2017-05-26 09:04:10

标签: android user-interface overlay

应用程序是否有办法:

  1. 检查屏幕叠加层是否存在,并
  2. 找出哪个包名称拥有叠加层?
  3. 我知道Android M及以上版本能够在权限页面中检测到屏幕叠加,并在检测到屏幕叠加时拒绝权限更改,但开发人员是否能够在应用层中实现相同的功能?

1 个答案:

答案 0 :(得分:0)

您可以通过在用户触摸您的MotionEvent.FLAG_WINDOW_IS_OBSCURED之一时检查View标志来检测覆盖。

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);

来源:Android M's settings app

但是,我认为无法检测出哪个应用拥有重叠广告。