关于 8.0以上WindowManager addView 报错 异常关于type类型设置

8.0版本如果设置
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;会报如下异常
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@d4c6880 -- permission denied for window type 2003
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:909)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:372)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:136)
解决办法:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else {
    layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}

你可能感兴趣的:(关于 8.0以上WindowManager addView 报错 异常关于type类型设置)