Prevent application with SYSTEM_ALERT_WINDOW from obscuring my application

[亡魂溺海] 提交于 2020-02-02 06:28:56

问题


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?

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.

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.


回答1:


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

Later can be implemented like so:

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

See also the Security section of View class documentation.

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.



来源:https://stackoverflow.com/questions/47177996/prevent-application-with-system-alert-window-from-obscuring-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!