title: Unable to add window -- permission denied for this window type
body: <p>i have use WindowManager in Service and i have been try to find resolving from <a href="https://stackoverflow.com/questions/7569937/unable-to-add-window-android-view-viewrootw44da9bc0-permission-denied-for-t">this</a> post and other post but i still get this error massage. </p>
  
  <blockquote>
    <p>Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type</p>
  </blockquote>
  
  <p>i already use <code>android.permission.SYSTEM_ALERT_WINDOW</code>&lt;--- current on my Manifest.xml or 
  <code>android.permission.INTERNAL_SYSTEM_WINDOW</code>(this is system app only can use)</p>
  
  <p>My Minimum sdk is API 17.</p>
  
  <p>error massage:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: id.nax.androidstatelite, PID: 16973
      java.lang.RuntimeException: Unable to create service id.nax.androidstatelite.FloatingWindow: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
          at android.app.ActivityThread.handleCreateService(ActivityThread.java:3883)
          at android.app.ActivityThread.access$2100(ActivityThread.java:229)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:148)
          at android.app.ActivityThread.main(ActivityThread.java:7406)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
       Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b285d2e -- permission denied for this window type
          at android.view.ViewRootImpl.setView(ViewRootImpl.java:936)
          at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
          at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
          at id.nax.androidstatelite.FloatingWindow.onCreate(FloatingWindow.java:56)
          at android.app.ActivityThread.handleCreateService(ActivityThread.java:3873)
          at android.app.ActivityThread.access$2100(ActivityThread.java:229) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:148) 
          at android.app.ActivityThread.main(ActivityThread.java:7406) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  </code></pre>
  
  <p>my Service: (i mean this code from download example project)</p>
  
  <pre><code>public class FloatingWindow extends Service {
  
      WindowManager wm;
      LinearLayout ll;
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onCreate() {
          // TODO Auto-generated method stub
          super.onCreate();
  
          wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  
          ll = new LinearLayout(this);
          ll.setBackgroundColor(Color.RED);
          LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.MATCH_PARENT, 400);
          ll.setBackgroundColor(Color.argb(66,255,0,0));
          ll.setLayoutParams(layoutParameteres);
  
          final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
                  500, 200, WindowManager.LayoutParams.TYPE_PHONE,
                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                  PixelFormat.TRANSLUCENT);
          parameters.gravity = Gravity.CENTER | Gravity.CENTER;
          parameters.x = 0;
          parameters.y = 0;
  
        Button     stop = new Button(this);
      stop.setText("Stop");
          ViewGroup.LayoutParams btnParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
          stop.setLayoutParams(btnParameters);
  
          ll.addView(stop);
          wm.addView(ll, parameters);
  
          ll.setOnTouchListener(new View.OnTouchListener() {
              WindowManager.LayoutParams updatedParameters = parameters;
              double x;
              double y;
              double pressedX;
              double pressedY;
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
  
                  switch (event.getAction()) {
                      case MotionEvent.ACTION_DOWN:
  
                          x = updatedParameters.x;
                          y = updatedParameters.y;
  
                          pressedX = event.getRawX();
                          pressedY = event.getRawY();
  
                          break;
  
                      case MotionEvent.ACTION_MOVE:
                          updatedParameters.x = (int) (x + (event.getRawX() - pressedX));
                          updatedParameters.y = (int) (y + (event.getRawY() - pressedY));
  
                          wm.updateViewLayout(ll, updatedParameters);
  
                      default:
                          break;
                  }
  
                  return false;
              }
          });
  
          stop.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  wm.removeView(ll);
                  stopSelf();
                  System.exit(0);
              }
          });
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          stopSelf();
      }
  
  } 
  </code></pre>
  
  <p>i have prefer / copy code from <a href="https://www.youtube.com/watch?v=kjGPE_XLmwg" rel="nofollow noreferrer">this</a> and <a href="https://drive.google.com/file/d/0ByusCMz9WiFQUFpzZm91T1VnY2M/view" rel="nofollow noreferrer">this</a>
  i copy this code its kinda old, there is already app-debug.apk (this use <code>android.permission.SYSTEM_ALERT_WINDOW</code> in the manifest) in the project download and i try its run normally but when i use the that code to my project its give me that error massage.</p>
  
  <p>what else permission i must use? </p>