Android.view.WindowManager $ BadTokenException: Cannot add window android.view.ViewRootImpl$W@3decac7 - permission denied for this window type

I have a window manager inside a broadcast receiver and I am using API> 23, so I have granted overlay permission in the main activity as shown below.

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        startActivity(myIntent);
    }

      

After granting permission, also get the following error.

 android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@3decac7 -- permission denied for this window type.

      

window manager used in broadcast receiver

 WindowManager.LayoutParams params = new 
 WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                            WindowManager.LayoutParams.MATCH_PARENT,
                            WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
                            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                            PixelFormat.TRANSLUCENT);
                    params.gravity= Gravity.CENTER;
                    params.x=0;
                    params.y=0;
                    windowManager.addView(look, params);

      

granted permission for Manifest as well.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

      

please help how to do this.

+3


source to share


1 answer


You have a bad layout type ( WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG

). You can use TYPE_APPLICATION_OVERLAY

for api> = 26 and TYPE_TOAST

for downstream api. Also you can read WindowManager.LayoutParams api doc file to see what type you can use



0


source







All Articles