Window level for the development of floating windows

Source: Internet
Author: User

Recently, there was a need to suspend your UI interaction (drag, input, and other complex UI interactions) on top of a specific other application, similar to a nine-swim floating window, but our experience is better than a nine-way tour, and we've crossed a lot of licensing restrictions.


Many people know how to achieve a simple floating window, but few people go deep into the process behind the mechanism, because the project in the window of the interaction is more complex, encountered some pits to view a lot of information, it summarizes the floating window involved in the Knowledge points:
* Window hierarchy (how is the floating window "floating")?
* What are the limitations of floating windows, how to realize the floating window function over user authorization?
* Windows and user input system (how does activity receive touch events)?

In this chapter we examine the first question: why floating windows float. Floating windows are called floating windows because they can be suspended from the application or the Desktop window, and can be separated from the activity and exist. To study the differences, let's take a look at how our most familiar activity is displayed.

How did the activity show up?

To find out the answer to this question, we first from the activity of the Setcontentview () This method of the source to start, in the activity to see the source of Setcententview:

public void setContentView(int layoutResID) {    getWindow().setContentView(layoutResID);    initWindowDecorActionBar();}

GetWindow is the Mwindow variable that returns the activity, pointing to the object of a window, which is an abstract class that returns a Phonewindow object (Phonewindow is a subclass of window). Phonewindow has a Decorview object, Decorview member, which is a Framelayout,setcontentview sub-layout that will eventually be added to Decorview, This decorview is the root view of the current window, how is this root view ultimately drawn? There is a piece of code in Activitythread:

            l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;            l.softInputMode |= forwardBit;            if (a.mVisibleFromClient) {                a.mWindowAdded = true;                wm.addView(decor, l);            }

This decorview will eventually be added to the drawing system by Windowmanager.addview, and the type is WindowManager.LayoutParams.TYPE_BASE_APPLICATION, This parameter determines the level of the z-axis of the window to be drawn, in order to avoid the thought stack too deep, here does not post the detailed source tracking process, directly to the conclusion.
Let's look at the relationship between activity and window:


Then the relationship between window and view:

Activity window Display Process:


It says how activity is displayed, in fact, how the view of activity management is displayed. Finally, let's summarize:

The view that activity is set through Setcontentview is added to Phonewindow's root view decor. Second, window is an abstract concept, window closes a view (root view), ultimately managed by WindowManager is a view (root view) and its layoutparams, The view drawing refreshes are drawn from the underlying WindowManager (Windowmanagerglobal) with the WINDOWMANAGERSERVICEIPC interaction call. Activity is the only component in the four components that is closely linked to the form (this is why beginners have a direct understanding of activity as the reason for drawing the interface), all the views in charge are just a window and dialog, Toast, The wallpaper does not have the same window type. Why does the floating window "float"?

In fact, the display of activity has revealed the general interface display process, the display process of the floating window is more simple:


The students who have done the floating window should understand, why the floating window can be separated from activity and show, in essence, we are a view to the WindowManager to manage, The Layoutparams.type type determines the type of the View display window, and the level of the window (z-axis) displayed in different types is not the same. In large terms can be divided into the application window (Application_window), Sub-window (sub_window), System window (System_window) three types, the application window Z-axis range is 1~99, the child window is the range of 1001~1999, The System window is (2000~2999), so to implement the floating window we can only be implemented in the scope of the system window.


Here we have a general understanding of the Windows hierarchy of the Android system, activity is one of the four components of Android application, describing the activity state and cycle of the application, managed by Activitymanagerservice Window/view is the abstract model of the graph window, which describes the drawing information of the window, which is managed by the Windowmanagerservice, and the Activity Aggregation window is associated with the graphical windows. The article aims to understand the Android form system of a prototype, limited ability can not track the entire window system of the source code, interested in their own in-depth, the next article: "Over the authorized use of floating windows."

Author statement: I only in Pinterest and my blog published articles, reproduced need to contact me to obtain consent! Readers reading when the layout of confusion, advertising and other impact of the reading experience is not the original, suggest to Jane or I blog to read the original to achieve a better reading experience.

Window level for the development of floating windows

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.