问题
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 yourView
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 forFLAG_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