An example analysis of the implementation principle of the floating window in Android _android

Source: Internet
Author: User
Tags documentation

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 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
@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) * *
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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.