title: Android System Overlay window not working
body: <p>I am building and Android app and trying to learn all I can in the process. As a test I wanted to add a "chat heads" style overlay to the system. So, I looked around and found some good tutorials, and some really good answers on here, but it is still not working. In my android manifest I have the uses-permission declaration..</p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  </code></pre>
  
  <p>Then in my Preference page, when the user clicks on a SwitchPreference to activate the popup chat window I am checking for version and permissions like so...</p>
  
  <pre><code>private Preference.OnPreferenceChangeListener chatHeadsChangeListener = new Preference.OnPreferenceChangeListener() {
          @Override
          public boolean onPreferenceChange(Preference preference, Object o) {
              if((boolean) o) {
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp;
                      !Settings.canDrawOverlays(getApplicationContext())) {
                      Intent intent = new Intent(
                              Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                              Uri.parse("package:" + getPackageName())
                      );
                      startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
                  } else {
                      initializeChatHeadsView();
                  }
              }
              return true;
          }
      };
  </code></pre>
  
  <p>Then in my onActivityResult I am checking again to make sure, like one answer I found on here...</p>
  
  <pre><code>@Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
              if(Settings.canDrawOverlays(this)) {
                  initializeChatHeadsView();
              } else {
                  Toast.makeText(
                          this,
                          "Draw over other app permission was not available. Cannot activate Popup Chat",
                          Toast.LENGTH_LONG
                  ).show();
              }
          }
      }
  </code></pre>
  
  <p>The first time I load the app up on the emulator, which is running Android 8.0, I go into my settings page and click the SwitchPreference turning it on. The settings page opens up asking me if I want to enable the permission, I activate the permission, but it doesn't go back to my app after activating, so I hit the back button which goes back into my app, then I get the "[Application] has stopped responding." If I check the log in AndroidStudio I get the error...</p>
  
  <pre><code>Process: test.notreal.justatext, PID: 21429
                    java.lang.RuntimeException: Unable to create service test.notreal.justatext.service.FloatingViewService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@bc855eb -- permission denied for window type 2002
  </code></pre>
  
  <p>So it is still saying the permission is denied, even though I am enabling the permission, and in my onActivityResult I am checking if(Settings.canDrawOverlays())</p>
  
  <p>Can anyone see why this is failing? I can't figure it out. Thank you for any help, I really appreciate it.</p>