HeyThere
HeyThere

Reputation: 583

TYPE_APPLICATION_OVERLAY permission denied 2038 on Android N?

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.

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

Answers (1)

shmakova
shmakova

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

Related Questions