首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

WindowManager兑现悬浮窗口

2012-08-28 
WindowManager实现悬浮窗口Android中悬浮窗口的实现原理和示例代码 http://www.xsmile.net/?p404 调用Win

WindowManager实现悬浮窗口
Android中悬浮窗口的实现原理和示例代码
http://www.xsmile.net/?p=404

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

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

而WindowManager.LayoutParams的属性就多了,非常丰富,具体请查看SDK文档。这里给出Android中的WindowManager.java源码,可以具体看一下。
WindowManager 的源码地址:
http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/view/WindowManager.java

以下代码请仅供演示:
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();  
        wmParams.type=2002;  //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  
    }  


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();
        wmParams.type=2002;  //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
    }
}

PS:本代码在Android2.3下测试无错(从API Level来看,实际上android1.5都可行)!另外别忘了在AndroidManifest.xml文件中加入如下权限:
Xml代码 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />


关于代码中wmParams.type的值的问题,下面给出一些数值参考:
/**
         * Window type: the status bar.  There can be only one status bar
         * window; it is placed at the top of the screen, and all other
         * windows are shifted down so they are below it.
         */
        public static final int TYPE_STATUS_BAR         = FIRST_SYSTEM_WINDOW;
   
        /**
         * Window type: the search bar.  There can be only one search bar
         * window; it is placed at the top of the screen.
         */
        public static final int TYPE_SEARCH_BAR         = FIRST_SYSTEM_WINDOW+1;
   
        /**
         * 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;
       
        /**
         * Window type: keyguard window.
         */
        public static final int TYPE_KEYGUARD           = FIRST_SYSTEM_WINDOW+4;
       
        /**
         * Window type: transient notifications.
         */
        public static final int TYPE_TOAST              = FIRST_SYSTEM_WINDOW+5;
       
        /**
         * Window type: system overlay windows, which need to be displayed
         * on top of everything else.  These windows must not take input
         * focus, or they will interfere with the keyguard.
         */
        public static final int TYPE_SYSTEM_OVERLAY     = FIRST_SYSTEM_WINDOW+6;
       
        /**
         * Window type: priority phone UI, which needs to be displayed even if
         * the keyguard is active.  These windows must not take input
         * focus, or they will interfere with the keyguard.
         */
        public static final int TYPE_PRIORITY_PHONE     = FIRST_SYSTEM_WINDOW+7;
       
        /**
         * Window type: panel that slides out from the status bar
         */
        public static final int TYPE_STATUS_BAR_PANEL   = FIRST_SYSTEM_WINDOW+8;
       
        /**
         * Window type: panel that slides out from the status bar
         */
        public static final int TYPE_SYSTEM_DIALOG      = FIRST_SYSTEM_WINDOW+8;
   
        /**
         * Window type: dialogs that the keyguard shows
         */
        public static final int TYPE_KEYGUARD_DIALOG    = FIRST_SYSTEM_WINDOW+9;
       
        /**
         * Window type: internal system error windows, appear on top of
         * everything they can.
         */
        public static final int TYPE_SYSTEM_ERROR       = FIRST_SYSTEM_WINDOW+10;
       
        /**
         * End of types of system windows.
         */
        public static final int LAST_SYSTEM_WINDOW      = 2999;

---------------------------------
这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上!

    可以看出来,2002的值的含义其实就是2000+2。数值2000的含义就是系统级窗口,2002和2003的区别就是 2003 比 2002还要更上一层!比如,type为2003的view,能显示在手机下拉状态栏之上!

而关于flags等其他的属性请参考SDK文档

热点排行