Windows, WindowManager and Window management for Android

Source: Internet
Author: User

This is a small piece of information on the content of the floating window studied two days ago. First of all is reprinted an introduction to the Android window content, I think write well: http://blog.csdn.net/xieqibao/article/details/6567814

To prevent loss, copy the content again (* ^__ ^ ......

In fact, windows and views are actually displayed to users in android. The role of activity in android is mainly to solve some logic problems, such as lifecycle management and window creation. In android, window management is an important part, because it is directly responsible for displaying the content to the user and interacting with the user. Respond to user input.

When talking about window management, it is necessary to first talk about the ViewManager interface, which mainly includes the following implementation sub-interfaces and implementation classes: WindowManager and ViewGroup there are three important methods:

  • AddView ();
  • UpdateViewLayout ();
  • RemoveView ();

In WindowManager, The addView method adds the top-level view (DecorView) in the main window to WindowManager and creates a session. Next, we will introduce it in detail. Let's take a look at Window.

Window:

Window is the Window in android, which indicates the top-level Window, that is, the main Window. It has two implementation classes: PhoneWindow and MidWindow. Generally, the activity corresponds to PhoneWindow, setContentView and other methods frequently used in the activity are also implemented in this.

(MContentParent = Callback cb = (cb! = Cb. onContentChanged ();}View Code

Each main window has a View, called DecorView, which is the top-level view (actually the ViewGroup) in the main window. There are two member variables in the View: mParent, mChildren, it is used to manage the parent-child relationship of a view. ViewGroup manages a group of views. Therefore, the relationship between all views is established in ViewGroup. The ViewGroup is attached to the main window. In this way, it is easy to find the specific View through findViewById in the window. Event Processing in view is also based on this path.

Let's take a look at two important methods in ActivityThead (ActivityThead will be detailed in an article ):

  • Define mlaunchactivity ();
  • HandleResumeActivity ();

In javasmlaunchactivity, the activity. attach method is called to create a window. When the activity is started in the handleResumeActivity method, the main window is added to WindowManager.

View decor = r. window. getDecorView (); ViewManager wm =. getWindowManager (); WindowManager. layoutParams l =. mDecor = l. type = l. softInputMode | =. mWindowAdded = wm. addView (decor, l );}View Code

Let's take a look at WindowManager.

WindowManager:

WindowManager is mainly used to manage the status, attributes, view addition, deletion, update, window order, and message Collection and Processing of windows.

You can use Context. getSystemService (Context. WINDOW_SERVICE) to obtain the WindowManager instance.

WindowManager is inherited from ViewManager, which involves three important window management methods:

  • AddView ();
  • UpdateViewLayout ();
  • RemoveView ();

There is also an important static class LayoutParams in WindowManager. It can be used to set and obtain some properties of the current window.

Let's take a look at the addView () method. In addView, LayoutParams is used to obtain the View attribute of the window and ViewRoot is created for each window. ViewRoot is a bridge between the View and WindowManager, to pass a View to WindowManager, you can use the setView () method of ViewRoot to transmit messages between View and WindowManager. When you add the main window to WindowManger, it first creates a proxy object:

Wm = (WindowManagerImpl) context. getSystemService (Context. WINDOW_SERVICE)

And enable the session (IWindowSession). Then, Window will establish a connection with WindowManager through this session,

Let's take a look at the setView method:

Res = mAdded = mView = mAttachInfo. mRootView = newRuntimeException ("Adding window failed "}View Code

In this Code, ViewRoot adds the window to WindowManager through IWindowSession. ViewRoot inherits Handler. In fact, ViewRoot is essentially a Handler. In a window, ViewRoot handles View event processing, message sending, and callback.

In this way, the window is added to WindowManager, and the window view, events, and message collection and processing are managed by WindowManager.

 

After knowing the principle, it is easy to add a floating window. The main key points are as follows:

1. First, add the permission to the AndroidManifest. xml file <uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW"/>

2. Obtain the WindowManager window mWindowManager = (WindowManager) getApplication (). getSystemService (getApplication (). WINDOW_SERVICE );

3. Set window properties:

WmParams. type = wmParams. format = wmParams. flags = wmParams. gravity = Gravity. LEFT | wmParams. x = 0 wmParams. y = 0 wmParams. width = wmParams. height = LayoutInflater inflater = mFloatLayout = (LinearLayout) inflater. inflate (R. layout. float_layout, mWindowManager. addView (mFloatLayout, wmParams );View 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.