如何检测我的活动何时被遮盖? [英] How to detect when my Activity has been obscured?

查看:93
本文介绍了如何检测我的活动何时被遮盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检测到我的活动是否被系统警报或其他覆盖物(例如,长按电源按钮时的电源菜单)遮盖了,或者检测到了启动我的活动.我注意到在这种情况下,前台应用程序仍将是我的应用程序,因此我不能简单地将其作为前台应用程序的基础.我还注意到,当我的活动被遮盖时,不会调用onPause(),因此我也无法在onPause()中放入任何逻辑.即使可以,我仍然必须区分系统警报/覆盖和用户按下后退"/主页"按钮.

我还有其他方法可以做到这一点吗?

解决方案

您可以检查是否隐藏了活动",片段"或视图".

对于活动,您需要覆盖 dispatchTouchEvent 方法,并检查事件是否具有标志 FLAG_WINDOW_IS_OBSCURED .有示例代码:

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

这是Android代码的一部分,请检查 onFilterTouchEventForSecurity 方法.有关更多信息,请参阅View文档的安全性部分. >

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. 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. 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. In order to check if Fragment is obscured, execute the following piece of code in Fragment that belongs to the OverlayTouchActivity activity:

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

Please see AppPermissionsFragment.java fragment (search for OverlayTouchActivity).

For View you should override onFilterTouchEventForSecurity method. For more information please see security section of View documentation.

这篇关于如何检测我的活动何时被遮盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆