其他分享
首页 > 其他分享> > Alert Dialog弹窗无法显示,报错Unable to add window android.view.ViewRootImpl$W. permission denied

Alert Dialog弹窗无法显示,报错Unable to add window android.view.ViewRootImpl$W. permission denied

作者:互联网

 Android 11上点击事件触发AlertDialog弹窗时,弹窗无法显示;后台打印提示Unable to add window android.view.ViewRootImpl$W. permission denied

代码中设置窗口类型时使用的TYPE_SYSTEM_ALERT,
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

  关于TYPE_SYSTEM_ALERT在源码中的解释

        /**
         * Window type: system window, such as low power alert. These windows
         * are always on top of application windows.
         * In multiuser systems shows only on the owning user's window.
         * @deprecated for non-system apps. Use {@link #TYPE_APPLICATION_OVERLAY} instead.
         */
        @Deprecated
        public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;

  该类型为系统级别窗口,用于低电量提示等信息显示,窗口显示在最顶部。对于非系统的app,应该使用TYPE_APPLICATION_OVERLAY替代。但是设置这个类型的情况下,可能会被系统弹窗覆盖掉,因此我们需要将它设置为既能显示在最顶部,也不会被其他系统弹窗覆盖的类型。

 

  我们可以使用TYPE_SYSTEM_OVERYLAY类型,同时也不会报错,可以在系统上正常显示弹窗。

/**
         * Window type: system overlay windows, which need to be displayed
         * on top of everything else.  These windows must not take input
         * focus, or they will interfere with the keyguard.
         * In multiuser systems shows only on the owning user's window.
         * @deprecated for non-system apps. Use {@link #TYPE_APPLICATION_OVERLAY} instead.
         */
        @Deprecated
        public static final int TYPE_SYSTEM_OVERLAY     = FIRST_SYSTEM_WINDOW+6;

 







标签:ViewRootImpl,permission,OVERLAY,SYSTEM,system,window,报错,TYPE,弹窗
来源: https://www.cnblogs.com/zoowei/p/16306335.html