开发者

how can i put my PopupWindow to system desk or other app?

开发者 https://www.devze.com 2023-01-16 18:41 出处:网络
How can I put my PopupWindow to the system desk or another app? How can I use this permission: INTERNAL_SYSTEM_WINDOW

How can I put my PopupWindow to the system desk or another app? How can I use this permission: INTERNAL_SYSTEM_WINDOW

An开发者_如何转开发y examples?


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);
  }
 }

}


The problem is resolved. I use WindowManager.addView in a service, and WindowManager.LayoutParams.type = TYPE_SYSTEM_ALERT, then it's ok!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号