title: SYSTEM_ALERT_WINDOW PERMISSION on API 26 not working as expected. Permission denied for window type 2002
body: <p>I am using overlay permission to display certain information in my app. Running it on API 23 - 25 it works fine (asking for permission, granting, etc. according to</p>
  
  <p><a href="https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t#answer-34061521">Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type</a>  ). (Many thanks to ceph3us!)</p>
  
  <p>Trying the same on API 26 I am getting an error, basically "permission denied for window type 2002" when calling</p>
  
  <pre><code>windowManager.addView(frameLayout, params);
  </code></pre>
  
  <p>Did Google change the way, overlay works? Any idea, how to get my text as an overlay onto the screen in Android 8 (Oreo), API 26?
  Thanks for your ideas!</p>
  
  <p>This is the error-log:</p>
  
  <pre><code>08-24 16:41:56.730 2615-2615/net.zwittscha.testoverlay E/AndroidRuntime: FATAL EXCEPTION: main
  Process: net.zwittscha.testoverlay, PID: 2615
  java.lang.RuntimeException: Unable to start activity ComponentInfo{net.zwittscha.testoverlay/net.zwittscha.testoverlay.MainActivity}: 
                  android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@6fa0089 -- 
                  permission denied for window type 2002
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
   at android.app.ActivityThread.-wrap11(Unknown Source:0)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
   at android.os.Handler.dispatchMessage(Handler.java:105)
   at android.os.Looper.loop(Looper.java:164)
   at android.app.ActivityThread.main(ActivityThread.java:6541)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
              Caused by: android.view.WindowManager$BadTokenException: 
              Unable to add window android.view.ViewRootImpl$W@6fa0089 -- 
              permission denied for window type 2002
   at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
   at net.zwittscha.testoverlay.MainActivity.createOnTopView(MainActivity.java:46)
   at net.zwittscha.testoverlay.MainActivity.checkDrawOverlayPermission(MainActivity.java:66)
   at net.zwittscha.testoverlay.MainActivity.onCreate(MainActivity.java:28)
   at android.app.Activity.performCreate(Activity.java:6975)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
   at android.app.ActivityThread.-wrap11(Unknown Source:0) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
   at android.os.Handler.dispatchMessage(Handler.java:105) 
   at android.os.Looper.loop(Looper.java:164) 
   at android.app.ActivityThread.main(ActivityThread.java:6541) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
  </code></pre>
  
  <p>In my Manifest, I have:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  
  &lt;uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION"/&gt;
  </code></pre>
  
  <p>This is my MainActivity:</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity {
  
  DrawView dv;
  FrameLayout frameLayout;
  WindowManager windowManager;
  LayoutInflater layoutInflater;
  /** code to post/handler request for permission */
  public final static int REQUEST_CODE = 1234;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      checkDrawOverlayPermission();
  }
  
  public void createOnTopView() {
  
      WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.TYPE_PHONE,
              WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
              PixelFormat.TRANSLUCENT);
      params.gravity = Gravity.CENTER;
  
      if (frameLayout == null) frameLayout = new FrameLayout(getApplicationContext());
      if (dv == null) dv = new DrawView(getApplicationContext());
  
      windowManager = (WindowManager)
              getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
      windowManager.addView(frameLayout, params);
      windowManager.addView(dv, params);
  
      layoutInflater = (LayoutInflater)
              getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      // Here is the place where you can inject whatever layout you want.
      layoutInflater.inflate(R.layout.activity_main, frameLayout);
  }
  
  public void checkDrawOverlayPermission() {
      /* check if we already  have permission to draw over other apps */
      if (android.os.Build.VERSION.SDK_INT &gt; 22) {
          if (!Settings.canDrawOverlays(this)) {
          /* if not construct intent to request permission */
              Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                      Uri.parse("package:" + getPackageName()));
          /* request permission via start activity for result */
              startActivityForResult(intent, REQUEST_CODE);
          }
          else {
              createOnTopView();
          }
      }
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
      /* check if received result code
       is equal our requested code for draw permission  */
      if (requestCode == REQUEST_CODE &amp;&amp; android.os.Build.VERSION.SDK_INT &gt; 22) {
     /* if so check once again if we have permission */
          if (Settings.canDrawOverlays(this)) {
              createOnTopView();
          }
      }
  }
  }
  </code></pre>
  
  <p>And this is the DrawView:</p>
  
  <pre><code>    public class DrawView extends View {
  
  int w;
  int h;
  int r;
  float screenFactor;
  TextPaint startTextPaint;
  
  public DrawView(Context activity) {
      super(activity);
  }
  
  @Override
  protected void onSizeChanged(int width, int height, int oldw, int oldh) {
  
          w = width;
          h = height;
          r = w / 2;
  
      screenFactor = (r / 160f);
  
      if (startTextPaint == null) startTextPaint = new TextPaint();
  
      startTextPaint.setTextSize(100);
      startTextPaint.setTextAlign(Paint.Align.CENTER);
      startTextPaint.setTypeface(Typeface.create("Roboto Condensed", Typeface.BOLD));
  
      super.onSizeChanged(w, h, oldw, oldh);
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
  
          startTextPaint.setARGB(255,255,0,0);
          canvas.drawText("Test", w / 2, h / 2, startTextPaint);
  }
  }
  </code></pre>