菜鸟的安卓实习之路---微信悬浮窗通知窗口怎么做怎么做?

这几天,每当微信有新的通知,不论你在任何一个页面,别的引用的页面也好,都会在屏幕顶部出现一个悬浮窗,来显示收到消息,而不是很low的通知栏,这是怎么做的呢?

其实就是现实一个系统级的窗口: 

核心代码:

public NotificationFloatingWindow(Context context, String operateMenu,
			String message) {
		this.inflater = LayoutInflater.from(context);
	<strong>	popView = (LinearLayout) inflater.inflate(R.layout.notification_layout,
				null);  //自己用xml定义窗口的样式</strong>
		popView.setOnClickListener(this);
		<strong>wm = (WindowManager) context.getApplicationContext().getSystemService(
				"window");  //</strong>
		this.context = context;
		init(operateMenu);
	}

	public NotificationFloatingWindow(Context context,
			NotificationReference reference) {
		this.inflater = LayoutInflater.from(context);
		popView = (LinearLayout) inflater.inflate(R.layout.notification_layout,
				null);
		popView.setOnClickListener(this);
		wm = (WindowManager) context.getApplicationContext().getSystemService(
				"window");
		this.context = context;
		init(reference);
	}
每次显示的时候调用:

WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
		<strong>wmParams.type = 2002; // type是关键,这里的2002表示系统级窗口,你也可以试试2003。</strong>
		wmParams.format = 1;
		wmParams.gravity = Gravity.TOP;
		wmParams.flags = 40;
		wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
	<strong>	wmParams.windowAnimations = R.style.notification_anim_style;   //还可以自定义弹出,消失动画</strong>
		wm.addView(popView, wmParams);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值