WMS Details (ii) How to understand the relationship between window and Windows? Based on Android7.0 source code

Source: Internet
Author: User

What is the token of the previous blog (WMS's detailed (a)? Based on the Android7.0 source code) We briefly introduced the role of token, which involves a lot of concepts, where the highest frequency of the number of Windows and window of the pair of partners, then we will today to see how we understand the Android system window and Windows.

The concept of the window, from a different point of view it does not have the same meaning, if we look at the window from the WMS (windowmanagerservice) Perspective, then this window is not a Windows class, but a view. The message sent by the user is not directly sent to each view after being received by the WMS, and the Iwindow class is actually receiving the user's message. The implementation class of the Iwindow class is the VIEWROOTIMPL.W class, each W class has a view variable inside, the WMS receives the message from the user, determines which window is active, finds the user message to the W class, and then passes the user message to the internal view variable, and the remaining thing is VI EW object to get it done. In general, in the WMS Eye, the window is a view, this article mentions that the window refers to a view;window is a further extraction and abstraction of the window behavior, window to the windows of some common behavior extracted from the unified processing, equivalent to window is a subset of Windows.

OK, the definition of the window is finished, so let's look at the type of window.

Window category

Depending on the Type property of the window, the window can be divided into three categories, the application window, the Sub-window and the System window, and the view is added through the AddView method in the Windowmanagerimpl class. The method eventually calls the AddView method in Windowmanagerglobal, and we'll look at this method:

public void AddView (view view, Viewgroup.layoutparams params, display display, Window ParentWindow) {if (view =    = null) {throw new IllegalArgumentException ("View must not is null");    } if (display = = null) {throw new IllegalArgumentException ("Display must not is null"); } if (! ( params instanceof Windowmanager.layoutparams) {throw new IllegalArgumentException ("params must be WINDOWMANAGER.L    Ayoutparams "); } final Windowmanager.layoutparams wparams = (windowmanager.layoutparams) params, ..../If This is a pane      L window, then find the window it's being//attached to for a future reference. if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && wparams.type <= Windowmana Ger.            Layoutparams.last_sub_window) {Final int count = Mviews.size ();           for (int i = 0; i < count; i++) {if (Mroots.get (i). Mwindow.asbinder () = = Wparams.token) {         Panelparentview = Mviews.get (i);        }}} root = new Viewrootimpl (View.getcontext (), display);        View.setlayoutparams (Wparams);        Mviews.add (view);        Mroots.add (root);    Mparams.add (Wparams); }    ............}

The method receives four parameters, the first parameter represents the view to be added, the second parameter represents the view's parameters, the third parameter represents the display device to be output, and the fourth parameter represents the view's parent layout. The second parameter params requirement must be an instance of Windowmanager.layoutparams, the parameter params has a parameter type is used to mark each window type, this type is actually an int value, this int value represents the window layer, WMS in the window overlay , when you assign different layers according to the int constant size, the larger the int value, the more the view is closer to the upper layer, and the smaller the int value, the closer the view is to the lower layer. And then we'll take a look at these three different types of Windows:

application window

The window of activity corresponds to the application window, but since the activity is loaded by AMS, the application window is actually created by AMS. All of the activity's default window types are type_application, as you can see from the Windowmanager.layoutparams construction method:

Public Layoutparams () {            super (layoutparams.match_parent, layoutparams.match_parent);            Type = type_application;            format = Pixelformat.opaque;        }

In this category, the system pre-defines several variables, let's take a look at:

According to this table the small partners can find that the application window layer value will not be greater than 99, and the system in the window overlay will automatically assign a different size of the window layer values.

child window

child window so as a child window is because it has a parent window, the parent window can be any type of other window, we in the development of common sub-windows have Popupwindow, Dialog, ContextMenu, PopupMenu and so on. About sub-windows, the system also defines several types, let's take a look at:

When we create a subwindow, we can specify that the window type is between 1000~1999, and the WMS dynamically adjusts the layer values when the window overlay is in progress.

System window

Common system windows are the status bar, navigation bar (the domestic part of the Android phone has), when the ANR when the prompt box, Input Method window, Toast window, lock screen display screensaver and a variety of the kind of the ball of the speed of the housekeeper. The system window does not need to have the corresponding activity window, it does not need the parent window, in most cases we see the System window is created by the system, of course, we can also create the system window ourselves, and the other two types of Windows, the system also helped us to create a part of the System window constants:

In fact, in Android7.0, the system defines more than 30 system window constants, but some of them are not currently used, so I'm not listing them here. When we create a system window, we can specify that the system window's layer value is between 2000-2999, and the WMS dynamically adjusts the layer value when the window overlay is in place, but the value is between 2000-2999.


Window creation

Having read a lot of theoretical knowledge, let's look at how we can create Windows ourselves.

Creating child windows

Suppose I have a button on the current page, and when I click on the button, a subwindow appears with the following effect:

Let's take a look at the code:

    public void Btnclick (view view) {        IBinder token = View.getwindowtoken ();        Windowmanager.layoutparams LP = new Windowmanager.layoutparams (0, 0, pixelformat.transparent);        Lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;        Lp.token = token;        Final TextView TV = new TextView (this);        Tv.settext ("I Am the popup subwindow");        Tv.setbackgroundcolor (Color.Blue);        WM = Getwindowmanager ();        Tv.setonkeylistener (New View.onkeylistener () {            @Override public            boolean OnKey (View v, int keycode, keyevent Event) {                if (keycode = = keyevent.keycode_back) {                    MainActivity.this.wm.removeView (TV);                }                return false;            }        });        This.wm.addView (TV, LP);    }

The code seems to be very difficult, so I won't explain it too much here.

Create a system window

The system window may be a lot of small partners using more window creation method, the common 360 suspension ball is implemented in this way. Here I also give a small example:

    public void AddView (view view) {LP = new Windowmanager.layoutparams (0, 0, pixelformat.transparent); Lp.flags =//window does not need to get focus, this property automatically turns on Flag_not_touch_modal WindowManager.LayoutParams.FLA                        g_not_focusable |                        Events within the current window area are handled by themselves, and events outside the region are handed over to the underlying window processing WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |        The lock screen state can also show window out WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; lp.gravity = Gravity.left |        Gravity.top;        Note that the reference point is the upper-left corner of the actionbar lp.x = 200;        LP.Y = 200; Type is used to describe window types, the window type is divided into three types://Application-level window (1-99), Sub-window (1000-1999), System window (2000-2999)//Level window will be overwritten        Low-level window Lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;        WindowManager = Getwindowmanager ();    Windowmanager.addview (TV, LP);    } public void Removeview (view view) {Windowmanager.removeview (TV); }

Here are two methods, one to add the window, and one to remove the window. But using system windows generally requires us to add permissions:

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

OK, the introduction of window and Windows so much, there are questions welcome message discussion.

Resources:

1. An analysis of Android windows


Above.




WMS Details (ii) How to understand the relationship between window and Windows? Based on Android7.0 source code

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.