如何在android中从服务启动popupwindow?

如何在android中从服务启动popupwindow?,android,Android,我希望创建一个浮动按钮,它将悬停在整个Android屏幕上。单击按钮或悬停图标时,应打开一个小的弹出窗口 我可以使用服务类创建悬停图标,但是我无法启动popupwindow。每当我单击浮动图标时,错误日志都会显示: Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? import

我希望创建一个浮动按钮,它将悬停在整个Android屏幕上。单击按钮或悬停图标时,应打开一个小的
弹出窗口

我可以使用服务类创建悬停图标,但是我无法启动
popupwindow
。每当我单击浮动图标时,错误日志都会显示:

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> ViewConfiguration.getLongPressTimeout() && 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;
  }
}

您不应该在服务类中执行这些ui操作。因为在显示按钮后,服务实例将被删除,并且单击按钮时没有上下文。我认为你应该遵循下面类似的方法

服务类别:

Intent activityIntent = new Intent(context, MainActivity);
activityIntent.putExtra("showPopup", true);
context.startActivity(activityIntent);
活动类别:

onCreate(){

   boolean showPopUp = getIntent().getBooleanExtra("showPopup", false);
   if(showPopUp){

   // Show your popup here

   }

}

您不应该在服务类中执行这些ui操作。因为在显示按钮后,服务实例将被删除,并且单击按钮时没有上下文。我认为你应该遵循下面类似的方法

服务类别:

Intent activityIntent = new Intent(context, MainActivity);
activityIntent.putExtra("showPopup", true);
context.startActivity(activityIntent);
活动类别:

onCreate(){

   boolean showPopUp = getIntent().getBooleanExtra("showPopup", false);
   if(showPopUp){

   // Show your popup here

   }

}

请显示您已尝试的内容。我已添加service.java,它将通过intent从mainactivity调用。请显示您已尝试的内容。我已添加service.java,它将通过intent从mainactivity调用