Android中悬浮窗口的实现原理和示例代码

用了我一个周末的时间,个中愤懑就不说了,就这个问题,我翻遍全球网络没有一篇像样的资料,现在将实现原理简单叙述如下:

调用WindowManager,并设置WindowManager.LayoutParams的相关属性,通过WindowManager的addView方法创建View,这样产生出来的View根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统*窗口,实现悬浮窗口效果!

WindowManager的方法很简单,基本用到的就三个addView,removeView,updateViewLayout。

而WindowManager.LayoutParams的属性就多了,非常丰富,具体请查看SDK文档。这里给出Android中的WindowManager.java源码,可以具体看一下。

下面是简单示例代码:

public class myFloatView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bb=new Button(getApplicationContext());
WindowManager wm=(WindowManager)getApplicationContext().getSystemService("window");
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams(); /**
*以下都是WindowManager.LayoutParams的相关属性
* 具体用途请参考SDK文档
*/
wmParams.type=2002; //这里是关键,你也可以试试2003
wmParams.format=1;
/**
*这里的flags也很关键
*代码实际是wmParams.flags |= FLAG_NOT_FOCUSABLE;
*40的由来是wmParams的默认属性(32)+ FLAG_NOT_FOCUSABLE(8)
*/
wmParams.flags=40;
wmParams.width=40;
wmParams.height=40;
wm.addView(bb, wmParams); //创建View
}
}
别忘了在AndroidManifest.xml中添加权限: PS:这里举例说明一下type的值的意思: /**
* Window type: phone. These are non-application windows providing
* user interaction with the phone (in particular incoming calls).
* These windows are normally placed above all applications, but behind
* the status bar.
*/
public static final int TYPE_PHONE = FIRST_SYSTEM_WINDOW+2; /**
* Window type: system window, such as low power alert. These windows
* are always on top of application windows.
*/
public static final int TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3;
这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上! ————————————————————————————- 已经给出可*移动悬浮窗口的Demo,参加这里。

http://blog.csdn.net/pipisky2006/article/details/6336845

上一篇:QT中将ASCII转换为对应数值的方法


下一篇:字符串和ASCII之间的转换