title: Android: Unable to add window. Permission denied for this window type
body: <p>I'm working on an app where I need to display a window with some info <strong>ON</strong> the Lock Screen (KeyGuard) without unlocking the phone. I figured I could probably do it with <a href="http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#TYPE_KEYGUARD_DIALOG" rel="noreferrer">WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG</a></p>
  
  <p>But every time my app crashes with the following error: </p>
  
  <blockquote>
    <p>android.view.WindowManager$BadTokenException: Unable to add window
    android.view.ViewRootImpl$W@40ec8528 -- permission denied for this
    window type</p>
  </blockquote>
  
  <p>These posts (<a href="https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t">here</a>, <a href="https://stackoverflow.com/questions/18400539/windowmanagerbadtokenexception-unable-to-add-window">here</a> and <a href="https://stackoverflow.com/questions/6103401/windowmanagerbadtokenexception-unable-to-add-window-android-view-viewrootw4">here</a>) all give the same answer.
  To add the following permission in the Manifest file.</p>
  
  <blockquote>
    <p>android.permission.SYSTEM_ALERT_WINDOW</p>
  </blockquote>
  
  <p>Solution that I have implemented but I am still getting the same error.
  Any idea of what I'm doing wrong?</p>
  
  <p>Here are the permissions in my manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.droidpilot.keyguardwindow" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="16"
      android:targetSdkVersion="21" /&gt;
  
  &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  </code></pre>
  
  <p>And this is the code I use to add the Window to the lock screen</p>
  
  <pre><code>WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
      LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      mView = mInflater.inflate(R.layout.lock_screen_notif, null);
  
      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG,
              WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                      | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                      | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
              PixelFormat.TRANSLUCENT
      );
  
      wm.addView(mView, params);
  </code></pre>
  
  <p>Anybody got any idea?</p>
  
  <p>P.S. I'm testing on an HTC Desire 620 DS running Android 4.4.2</p>