Reputation: 583
I need to draw a system overlay view from a service. This is the part that adds this view to WindowManager:
WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
It failed with permission denied exception:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f15e8de -- permission denied for window type 2038
I have tried everything I found online and still not able to get this permission to pass.
Settings.canDrawOverlays
and it is true.Draw over other apps
permission enabled in App Settings.I looked at all the answers in Android: permission denied for window type 2038 using TYPE_APPLICATION_OVERLAY but none of them solve my problem.
Any help is appreciated! Thanks!
Upvotes: 0
Views: 4708
Reputation: 6426
You should choose another type for Android N:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
flags = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
flags,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
Upvotes: 3