使用 SYSTEM_ALERT_WINDOW 防止应用程序遮挡我的应用程序 [英] Prevent application with SYSTEM_ALERT_WINDOW from obscuring my application

查看:255
本文介绍了使用 SYSTEM_ALERT_WINDOW 防止应用程序遮挡我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以确保我的应用程序的窗口不会被任何其他应用程序的视图遮挡,并使用 SYSTEM_ALERT_WINDOW 权限?

Is there any way to assure that my application's window is not obscured by any other application's view using with SYSTEM_ALERT_WINDOW permission?

如果没有,那么除了获得相同的许可和刷新/显示/任何我自己的视图(当然显示在警报窗口中)之外,还有什么更好的方法可以确保我的应用程序不会被此类窗口遮挡100 毫秒 左右以使其可见?

If not, then is there any better way to assure that my app is not obscured by such window apart from obtaining the same permission and refreshing/showing/whatever my own view (of course shown in alert window) every 100ms or so to keep it visible?

最终闪烁,以防我的应用程序被遮挡,实际上是一件好事,并且向用户表明出现了问题.

Eventual flickering, in case my application is obscured, is actually a good thing and an indicator to the user that something is wrong.

编辑:似乎没有办法做到这一点,除非通过三星上的 KNOX 或其他一些可信任 UI 的专有解决方案.接受的答案足以满足我的目的,但它不是所提出问题的答案.

EDIT: It seems that there is no way to do it except from going through KNOX on Samsung or some other proprietary solution for Trusted UI. Accepted answer is enough for my purpose, but it is not an answer for the question asked.

推荐答案

即使这不是您要问的内容,我所知道的最接近的替代方案是:

Even if it's not exactly what you're asking, the closest replacement I know of is:

  • Either setting android:filterTouchesWhenObscured="true" in your layout (touch events will be filtered and not reach your View if they are going through an overlay, regardless is transparent or opaque). See View#setFilterTouchesWhenObscured(boolean),
  • Or overriding View#onFilterTouchEventForSecurity(android.view.MotionEvent) and checking for FLAG_WINDOW_IS_OBSCURED. See View#onFilterTouchEventForSecurity(android.view.MotionEvent).

以后可以这样实现:

override fun onFilterTouchEventForSecurity(event: MotionEvent): Boolean {
    if ((event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED) == MotionEvent.FLAG_WINDOW_IS_OBSCURED) {
        Toast.makeText(context, "Screen overlay detected!", Toast.LENGTH_LONG).show()
        return false // touch event is cancelled
    }
    return super.onFilterTouchEventForSecurity(event)
}

另请参阅 View 类文档的安全部分.

See also the Security section of View class documentation.

请注意,此功能可从 API 9+ 中获得.可以在此 SO 问题中找到旧 API 的解决方法:Analogue of android:filterTouchesWhenObscured API 级别低于 9.

Notice that this functionality is available from API 9+. A workaround for older APIs can be found in this SO Question: Analogue of android:filterTouchesWhenObscured for API level below 9.

这篇关于使用 SYSTEM_ALERT_WINDOW 防止应用程序遮挡我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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