Android 8.0 & Dialog & permission denied for window type 2003

Android 8.0 Dialog 需要适配,否则会报错闪退:

 android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@e283326 -- permission denied for window type 2003
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:905)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:372)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:128)
        at android.app.Dialog.show(Dialog.java:454)
        at com.maple.recorder.player.PlayDialog.showDialog(PlayDialog.java:141)
        at com.maple.recorder.player.PlayDialog.showDialog(PlayDialog.java:136)
        at com.maple.recordwav.ui.PlayPage.dialogPlay(PlayPage.java:123)
        at com.maple.recordwav.ui.PlayPage.access$200(PlayPage.java:37)
        at com.maple.recordwav.ui.PlayPage$2.onItemClick(PlayPage.java:92)
        at android.widget.AdapterView.performItemClick(AdapterView.java:321)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1217)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)

解决方案:

将Type类型

getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

改为如下,即可

getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);

参考文章

《android 8.0使用悬浮窗时,报错permission denied for window type 2007》

你可能感兴趣的:(Android 8.0 & Dialog & permission denied for window type 2003)