我如何将我的PopupWindow放到系统桌面或其他应用程序?

时间:2010-09-17 09:27:12

标签: android

如何将我的PopupWindow放入系统桌面或其他应用程序? 我如何使用此权限:INTERNAL_SYSTEM_WINDOW

任何例子?

2 个答案:

答案 0 :(得分:5)

public class PopupService extends Service implements IPopupTouchPosListener
{
 public final static String FIELD_EVENT = "event";
 /**
  * 开启
  */
 public final static int EVENT_SWITCH_ON = 1;

 /**
  * 关闭
  */
 public final static int EVENT_SWITCH_OFF = 2;

 private CustomPopupView mPopupView;
 private int mCurrentY;

 private RelativeLayout mLayout;

 private WindowManager mWindowManager;
 private WindowManager.LayoutParams mWmlp;

 @Override
 public void onCreate()
 {
  // TODO Auto-generated method stub
  super.onCreate();

  mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);

  // 获取屏幕宽度
  DisplayMetrics outMetrics = new DisplayMetrics();
  mWindowManager.getDefaultDisplay().getMetrics(outMetrics);
  int width = outMetrics.widthPixels;

  mWmlp = new WindowManager.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  mWmlp.alpha = 0.5f;
  mWmlp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 不能抢占聚焦点
  mWmlp.flags = mWmlp.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
  mWmlp.flags = mWmlp.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; // 排版不受限制
  mWmlp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;     // 系统提示类型
  mWmlp.width = width;
  mWmlp.height = 30;
  mWmlp.gravity = 2;
  mWmlp.format = -1;
  mWmlp.token = null;
  mWmlp.x = 0;
  mWmlp.y = 200;

  mCurrentY = mWmlp.y;

  mLayout = new RelativeLayout(this);
  mLayout.setBackgroundColor(0x00FFFFFF);
  mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

  mPopupView = new CustomPopupView(this);
  mPopupView.setTouchPosListener(this);
  RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 30);

  lp.bottomMargin = 0;
  lp.leftMargin = 0;

  mLayout.addView(mPopupView, lp);
 }

 @Override
 public void onStart(Intent intent, int startId)
 {
  super.onStart(intent, startId);

  Log.i(Constants.LOG_TAG, "popup service onstart");

  String action = intent.getAction();
  if(action != null && action.equals(ActionDef.ACTION_POPUP_SERVICE))
  {
   Bundle bundle = intent.getExtras();
   if(bundle != null)
   {
    int event = bundle.getInt(FIELD_EVENT);
    if(event == EVENT_SWITCH_ON)
    {
     // 显示
     mWindowManager.addView(mLayout, mWmlp);
    }
    else
    {
     mWindowManager.removeView(mLayout);
    }
   }
  }
 }

 @Override
 public IBinder onBind(Intent intent)
 {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public void onMove(float x, float y)
 {
  if(mLayout != null)
  {
   Log.i(Constants.LOG_TAG, "y = " + y);
   if(y > mLayout.getHeight())
   {
    mCurrentY = (int)y + mCurrentY - mLayout.getHeight();
   }
   else if(y < 0)
   {
    mCurrentY = (int)y + mCurrentY;
   }

   // 只更新位置
   mWmlp.y = mCurrentY;
   mWindowManager.updateViewLayout(mLayout, mWmlp);
  }
 }

}

答案 1 :(得分:0)

问题得到解决。我在服务中使用WindowManager.addViewWindowManager.LayoutParams.type = TYPE_SYSTEM_ALERT,然后就可以了!