This article describes the implementation principle of the floating window in Android. Share to everyone for your reference. Specifically as follows:
Spent a weekend of my time, the anger does not say, on this issue, I go over the global network does not have a decent data, now the principle of implementation is briefly described as follows:
Call WindowManager, and set Windowmanager.layoutparams related properties, through the WindowManager AddView method to create view, so that the resulting view according to the Windowmanager.layoutparams properties of different, the effect is different. For example, create a system top-level window, to achieve the suspension window effect!
WindowManager method is very simple, the basic use of the three addview,removeview,updateviewlayout.
And Windowmanager.layoutparams's property is more, very rich, please view the SDK documentation. Here gives the Android Windowmanager.java source code, you can specifically look.
The following is a simple sample code:
The public class Myfloatview extends activity {/** called ' when the ' activity is ' is a
-created./
@Override public Voi D 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 ();
/** * Below are the relevant properties of the Windowmanager.layoutparams * For specific purposes please refer to the SDK documentation
/wmparams.type=2002; This is the key, you can try 2003
Wmparams.format=1; /** * Here the flags are also very critical * code is actually wmparams.flags |= flag_not_focusable; The origin of *40 is Wmparams's default attribute (s) + flag_not_focusable (8) * *
wmparams.flags=40;
wmparams.width=40;
wmparams.height=40;
Wm.addview (BB, wmparams);//CREATE View
}
}
Don't forget to add permissions to the Androidmanifest.xml:
Copy Code code as follows:
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
PS: Here is an example of the meaning of the value of type:
/** * Window Type:phone. These are non-application windows providing * user interaction with the phone (in particular incoming). * 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 the top of application windows. * * Public
static final int type_system_alert = first_system_window+3;
The value of this First_system_window is 2000. The difference between 2003 and 2002 is that the 2003 type view is higher than the 2002 type, and can be displayed on the System dropdown status bar!
I hope this article will help you with your Android program.