android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl





 在安卓8.0 上如果使用windowManager的悬浮窗口的话 在安卓8.0上会出下面的错误:

 android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@a3c3b78 -- permission denied for window type 2003


权限申请失败,当然大家应该知道这个权限是代码动态申请的

可以在代码中跳转到

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent,100);

但是授权之后还是会报错的,原因是安卓8.0上的问题


//for android 7.0
 mParams.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
 //for android 8.0
// mParams.type=WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;

和安卓8.0上有关


for apps targeting Android 8.0

apps must use a new window type called TYPE_APPLICATION_OVERLAY.

so for Android 8.0 you must ask this permission, for lower verison

SYSTEM_ALERT_WINDOW permission

will work

For more Refer this link

https://developer.android.com/about/versions/oreo/android-8.0-changes.html#cwt


在安卓8.0上应该使用这个就可以解决了

WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

你可能感兴趣的:(android)