title: How to start popupwindow from service in android?
body: <p>I wish to create a floating button which will hover over the entire Android screen. When the button or hovering icon is clicked, it should open a small <code>popupwindow</code>. </p>
  
  <p>I was able to create the hovering icon using service class however I am unable to launch <code>popupwindow</code>. Whenever I click the floating icon, the error log says:</p>
  
  <pre><code>Caused by: android.view.WindowManager$BadTokenException: Unable to add window --  token null is not valid; is your activity running?
  
  
  import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.os.IBinder;
  import android.support.annotation.Nullable;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewConfiguration;
  import android.view.ViewGroup;
  import android.view.WindowManager;
  import android.view.WindowManager.LayoutParams;
  import android.widget.ImageView;
  import android.widget.PopupWindow;
  
       /**
          * Created by sejal on 17-Oct-15.
      */
    public class bubble extends Service{
  
   PopupWindow popw;
  
  private WindowManager windowManager;
  private ImageView floatingFaceBubble;
  
  
  
  public void onCreate() {
      super.onCreate();
      floatingFaceBubble = new ImageView(this);
      //a face floating bubble as imageView
      floatingFaceBubble.setImageResource(R.drawable.bubble);
  
      windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
      //here is all the science of params
      final LayoutParams myParams = new LayoutParams(
              LayoutParams.WRAP_CONTENT,
              LayoutParams.WRAP_CONTENT,
              LayoutParams.TYPE_PHONE,
              LayoutParams.FLAG_NOT_FOCUSABLE,
              PixelFormat.TRANSLUCENT);
      myParams.gravity = Gravity.TOP | Gravity.LEFT;
      myParams.x=0;
  
      // add a floatingfacebubble icon in window
  
  
      windowManager.addView(floatingFaceBubble, myParams);
      try{
  
  
          floatingFaceBubble.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
               getpopuppwindow();
  
              }
          });
  
  
  
          //for moving the picture on touch and slide
          floatingFaceBubble.setOnTouchListener(new View.OnTouchListener() {
              LayoutParams paramsT = myParams;
              private int initialX;
              private int initialY;
              private float initialTouchX;
              private float initialTouchY;
              private long touchStartTime = 0;
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  //remove face bubble on long press
                /*  if(System.currentTimeMillis()-touchStartTime&gt; ViewConfiguration.getLongPressTimeout() &amp;&amp; initialTouchX== event.getX()){
               ////       windowManager.removeView(floatingFaceBubble);
               ////       stopSelf();
               //       return false;
               //   }  */
                  switch(event.getAction()){
                      case MotionEvent.ACTION_DOWN:
                          touchStartTime = System.currentTimeMillis();
                          initialX = myParams.x;
                          initialY = myParams.y;
                          initialTouchX = event.getRawX();
                          initialTouchY = event.getRawY();
                          break;
                      case MotionEvent.ACTION_UP:
                          break;
                      case MotionEvent.ACTION_MOVE:
                          myParams.x = initialX + (int) (event.getRawX() - initialTouchX);
                          myParams.y = initialY + (int) (event.getRawY() - initialTouchY);
                          windowManager.updateViewLayout(v, myParams);
                          break;
                  }
                  return false;
              }
          });
      } catch (Exception e){
          e.printStackTrace();
      }
  }
  //(ViewGroup) floatingFaceBubble.findViewById(R.id.pop)
  
  
  private void getpopuppwindow() {
  
      try {
  
          System.out.print("111111");
          LayoutInflater inflater = (LayoutInflater)bubble.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          System.out.print("222222");
          View layout = inflater.inflate(R.layout.popup,(ViewGroup) floatingFaceBubble.findViewById(R.id.pop));
  
          System.out.print("333333");
          popw = new PopupWindow(layout ,300,100 ,true) ;
          System.out.print("444444");
          popw.showAtLocation(layout, Gravity.CENTER,0,0);
  
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  
  }
  
  
  
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
  
      return null;
    }
  }
  </code></pre>