title: Unable to add window android.view.ViewRootImpl$W@b65df5f -- permission denied for window type 2010
body: <p>Hi im getting this exception:</p>
  
  <blockquote>
    <p>Unable to add window android.view.ViewRootImpl$W@b65df5f -- permission
    denied for window type 2010</p>
  </blockquote>
  
  <p>I already added <code>android.permission.SYSTEM_ALERT_WINDOW</code></p>
  
  <p>Here is my manifest file</p>
  
  <p>
  </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  
  &lt;application
      android:hardwareAccelerated="true"
      android:largeHeap="true"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;receiver
          android:name="com.lockscreen.wipe.MainActivity$DeviceAdminSampleReceiver"
          android:description="@string/sample_device_admin_description"
          android:label="@string/sample_device_admin"
          android:permission="android.permission.BIND_DEVICE_ADMIN"&gt;
          &lt;meta-data
              android:name="android.app.device_admin"
              android:resource="@xml/device_admin_sample" /&gt;
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /&gt;
              &lt;action android:name="android.intent.action.SCREEN_ON" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;receiver
          android:name="com.lockscreen.wipe.LockscreenIntentReceiver"
          android:enabled="true" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;action android:name="android.intent.action.SCREEN_ON" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
  ...
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Java:</p>
  
  <pre><code>public class LockscreenUtils {
  
      // Member variables
      private OverlayDialog mOverlayDialog;
      private OnLockStatusChangedListener mLockStatusChangedListener;
  
      // Interface to communicate with owner activity
      public interface OnLockStatusChangedListener
      {
          public void onLockStatusChanged(boolean isLocked);
      }
  
      // Reset the variables
      public LockscreenUtils() {
          reset();
      }
  
      // Display overlay dialog with a view to prevent home button click
      public void lock(Activity activity) {
          if (mOverlayDialog == null) {
              mOverlayDialog = new OverlayDialog(activity);
              mOverlayDialog.show();
              mLockStatusChangedListener = (OnLockStatusChangedListener) activity;
          }
      }
  
      // Reset variables
      public void reset() {
          if (mOverlayDialog != null) {
              mOverlayDialog.dismiss();
              mOverlayDialog = null;
          }
      }
  
      // Unlock the home button and give callback to unlock the screen
      public void unlock() {
          if (mOverlayDialog != null) {
              mOverlayDialog.dismiss();
              mOverlayDialog = null;
              if(mLockStatusChangedListener!=null)
              {
                  mLockStatusChangedListener.onLockStatusChanged(false);
              }
          }
      }
  
      // Create overlay dialog for lockedscreen to disable hardware buttons
      private static class OverlayDialog extends AlertDialog {
  
          public OverlayDialog(Activity activity) {
              super(activity, R.style.OverlayDialog);
              WindowManager.LayoutParams params = getWindow().getAttributes();
              params.type = LayoutParams.TYPE_SYSTEM_ERROR;
              params.dimAmount = 0.0F;
              params.width = 0;
              params.height = 0;
              params.gravity = Gravity.BOTTOM;
              getWindow().setAttributes(params);
              getWindow().setFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_NOT_TOUCH_MODAL,
                      0xffffff);
              setOwnerActivity(activity);
              setCancelable(false);
          }
      // consume touch events
      public final boolean dispatchTouchEvent(MotionEvent motionevent) {
          return true;
      }
  
  }
  }
  </code></pre>
  
  <p>LockscreenUtils.lock() throws the exception...</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                    Process: com.lockscreen.wipe, PID: 8803
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lockscreen.wipe/com.lockscreen.wipe.LockScreenActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@ffd0980 -- permission denied for window type 2010
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                        at android.app.ActivityThread.-wrap11(Unknown Source:0)
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                        at android.os.Handler.dispatchMessage(Handler.java:106)
                        at android.os.Looper.loop(Looper.java:164)
                        at android.app.ActivityThread.main(ActivityThread.java:6494)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@ffd0980 -- permission denied for window type 2010
                        at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
                        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                        at android.app.Dialog.show(Dialog.java:330)
                        at com.lockscreen.wipe.LockscreenUtils.lock(LockscreenUtils.java:33)
                        at com.lockscreen.wipe.LockScreenActivity.lockHomeButton(LockScreenActivity.java:178)
                        at com.lockscreen.wipe.LockScreenActivity.onCreate(LockScreenActivity.java:75)
                        at android.app.Activity.performCreate(Activity.java:7009)
                        at android.app.Activity.performCreate(Activity.java:7000)
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                        at android.os.Looper.loop(Looper.java:164) 
                        at android.app.ActivityThread.main(ActivityThread.java:6494) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
  </code></pre>