Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
π About the author
As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!
Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)
Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!
π premise
WindowManager suspension window we should often use, in this I do not say his specific implementation, also do not talk about his source analysis.
Mainly discusses the permission adaptation, hierarchy introduction, and possible bugs.
π Practice
π hierarchy
The floating layer essentially sends the View to The WindowManager to play with. It sets the view to different types, so there are different levels of display (z-axis), and different services at different prices.
Generally speaking, there are three types: APPLICATION_WINDOW, SUB_WINDOW and SYSTEM_WINDOW. The z-axis range of application window is 1 ~ 99, the range of subwindow is 1001 ~ 1999, and the system window is (2000 ~ 2999).
Let’s do the whole thing:
It seems so obvious, I thought I’d be able to do a lot of inexplicable things with this, but Android has proved once again that it can slap you in the face and make you call you Dad.
For example: permission problem, mobile phone Android version is different, some Type you can not use. No suspense, even if you apply for hover permission.
πAndroid 7.1.1 or later
Apply for hover window permissions except for TYPE_TOAST, otherwise:
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@8d2124d -- permission denied for this window type
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2887)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Copy the code
You think you’re done asking for permission? No way. And:
android.view.WindowManager$BadTokenException: Unable to add window -- window android.view.ViewRootImpl$W@363f7b1 has already been added
at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
at android.widget.Toast$TN.handleShow(Toast.java:434)
at android.widget.Toast$TN$2.handleMessage(Toast.java:345)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Copy the code
In the official Android version update, this permission has been greatly adjusted, and Google has controlled it to prevent the misuse of application suspension window to cause various interference and affect the experience.
π Solve the problem
- Apply for hover window permissions, or use a lower-level TYPE_PHONE hover window.
if (Build.VERSION.SDK_INT > 24) {
wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
} else {
wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
}
Copy the code
-
Applications with the SYSTEM_ALERT_WINDOW privilege above System 8.0 can no longer use the following window types to display alert Windows above other applications and system Windows:
- TYPE_PHONE
- TYPE_PRIORITY_PHONE
- TYPE_SYSTEM_ALERT
- TYPE_SYSTEM_OVERLAY
- TYPE_SYSTEM_ERROR
-
Instead, the application must use a new window type called TYPE_APPLICATION_OVERLAY.
-
Build. Gradle with no more than 7.0 SDK versions
-
Come up with a better way to do it and then go back to the product or design
π other
π’ author: Kom and Kom in Kom
π’ reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)
π’ welcome to like π collect π message π