简体   繁体   English

如何检测我的活动何时被遮挡?

[英]How to detect when my Activity has been obscured?

I would like to be able to detect if my Activity has been obscured by, say, a system alert or some other overlay (for example the power menu when I long press on the power button), or some malware that detects the launch of my Activity. 我希望能够检测我的活动是否被系统警报或其他覆盖(例如我长按电源按钮时的电源菜单)或某些检测到我的启动的恶意软件所掩盖活动。 I noticed that the foreground app in this case would still be my app, so I can't simply base it on what the foreground app is. 我注意到在这种情况下前台应用程序仍然是我的应用程序,所以我不能简单地将它基于前台应用程序。 I also notice that onPause() isn't called when my Activity is obscured, so I can't put any logic in onPause() either. 我还注意到当我的Activity被遮挡时没有调用onPause(),所以我也不能在onPause()中放入任何逻辑。 Even if I can though, I would then have to differentiate between a system alert/overlay and the user pressing the back/home button. 即使我可以,但我必须区分系统警报/覆盖和用户按后/主页按钮。

Are there any other ways for me to accomplish this? 我还有其他方法可以做到这一点吗?

You can check if Activity, Fragment or View is Obscured. 您可以检查是否隐藏了活动,片段或视图。

For Activity you need override dispatchTouchEvent method and check if event has flag FLAG_WINDOW_IS_OBSCURED . 对于Activity,您需要覆盖dispatchTouchEvent方法并检查事件是否具有标志FLAG_WINDOW_IS_OBSCURED There is example code: 有示例代码:

public class OverlayTouchActivity extends Activity {
    private boolean mObscuredTouch;

    public boolean isObscuredTouch() {
      return mObscuredTouch;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
      mObscuredTouch = (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0;
      return super.dispatchTouchEvent(event);
    }
}

This is a part of Android code, please check OverlayTouchActivity.java . 这是Android代码的一部分,请检查OverlayTouchActivity.java In order to check if Fragment is obscured, execute the following piece of code in Fragment that belongs to the OverlayTouchActivity activity: 为了检查Fragment是否被遮挡,请在Fragment中执行属于OverlayTouchActivity活动的以下代码:

OverlayTouchActivity activity = (OverlayTouchActivity) getActivity();
if (activity.isObscuredTouch()) {
    // Fragment is bbscured
}

Please see AppPermissionsFragment.java fragment (search for OverlayTouchActivity ). 请参阅AppPermissionsFragment.java片段(搜索OverlayTouchActivity )。

For View you should override onFilterTouchEventForSecurity method. 对于View,您应该重写onFilterTouchEventForSecurity方法。 For more information please see security section of View documentation. 有关详细信息,请参阅View文档的安全性部分

You can use the PackageManager to query whose of the installed packages has suspect permissions like SYSTEM_ALERT_WINDOW, BIND_ACCESSIBILITY_SERVICE or BIND_DEVICE_ADMIN. 您可以使用PackageManager查询已安装的软件包中的哪些软件具有可疑权限,如SYSTEM_ALERT_WINDOW,BIND_ACCESSIBILITY_SERVICE或BIND_DEVICE_ADMIN。

Some code ideas 一些代码的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM