Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the Management of Window in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to understand the management of Window in Android. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Understand Android's Window

Window represents the concept of a window, is an abstract concept, each Window corresponds to a View and a ViewRootImpl,Window and View to establish a relationship through ViewRootImpl, so Window does not actually exist, it exists in the form of View.

Each window View in Android has a corresponding Window, such as Activity and Dialog. When they initialize, a corresponding PhoneWindow is created and assigned to a reference within it.

The hierarchy of window

WindowLayoutParams.setType Settin

Each window has its corresponding level. The application window is 1-99, the sub-window is 1000-1999, and the system window is 2000-2999. The high-level ones will cover the low-level ones.

The child window must depend on the parent window, for example, Dialog must pop up in Activity, window in Dialog is the child window, and window in Activity is the parent window

Permissions are required to display system-level window

Flags of WindowLayoutparams

FLAG_NOT_FOUCSABLE window does not need to gain focus or receive various input events. Enabling FLAG_NOT_TOUCH_MODALFLAG_NOT_TOUCH_MODAL at the same time will pass click events outside the current Window area to the underlying Window, while events in the current Window area will be handled by themselves.

FLAG_SHOW_WHEN_LOCKED turns on this mode so that window is displayed on the lock screen interface

2. Understand WindowManager in Android

The management of Window in Android is done through WindowManager. After creating PhoneWindow, you will also set WindowManager for the Window object. WindowManager is an interface that inherits the ViewManager interface. From this, you can see that the operation on Window is actually the operation on View. The implementation class of WindowManager is WindowMangerImpl, and WindowMangerImpl is created through new.

III. The relationship between Window and WindowManagerImpl

The WindowManagerImpl instance can be obtained through the getSystemService of ContextImpl, and the same ContextImpl gets the same WindowManagerImpl object. After getting the WindowMangerImpl, call the setWindowManager method of Window to establish the relationship between Window and WindowManagerImpl.

The main task in setWindowManager is to re-create a WindowManagerImpl bound to the current Window on the basis of the WindowManagerImpl instance, and assign a value to the attribute mWindowManager in Window.

In other words, Window establishes the first step relationship with WindowManager on the Java layer, and assigns the WindowManager in Activity, Dialog, and so on to a new WindowManagerImpl object.

Note: here we use singleton WindowManagerImpl, combine different Window, and finally construct a non-singleton WindowManagerImpl object associated with Window.

Fourth, the operation of Window

1. Add operation WindowManagerImpl.addView, note that it is to add a new Window, not to operate on a view in Window

Every window displayed in Android is actually the process of displaying View to the screen. If we customize a layout to be displayed and get the View object, all we have to do is call the addView method of the WindowManagerImpl object. The WindowManagerImpl instance can be obtained through the getSystemService of ContextImpl.

WindowManagerImpl object. There is a singleton WindowManagerGlobal object in WindowManagerImpl. In each method of WindowManagerImpl, the execution process of the task is passed to WindowManagerGlobal. In the process of transmission, not only the View and LayoutParams are passed, but also the associated window objects in WindowManagerImpl are also passed.

AddView method of WindowManagerGlobal

Judge the validity of parameters such as view and LayoutParams in WindowMAnagerGlobal, create ViewRootImpl, add ViewRootImpl, View and LayoutParams to the corresponding ArayyList set in WindowMAnagerGlobal, and then call the setView method of ViewRootImpl

ViewRootImpl

Inherited from Handler class, it acts as a bridge between native layer and Java layer View system.

When ViewRootImpl is created, a reference to the thread that created it is saved. When the View is updated during development, it will determine whether the current thread is the thread that created the ViewRootImpl, and if not, an exception will be thrown.

Generally, the ViewRootImpl is created in the main thread, so updating the UI in the child thread throws an exception because the ViewRootImpl is created in the UI thread, not because only the UI thread can update the UI

If the UI is modified in the child thread before the onResume of the Activity, no exception will be thrown, because the ViewRootImpl is created after the onResume, and then the update UI needs to be updated by ViewRootImpl. The screen of the Activity does not display before the onResume. Modifying the UI operation will only modify the UI in the layout, and will not call the method of ViewRootImpl to display on the screen.

Therefore, it is concluded that only after the UI is displayed on the screen, when the UI is updated, it will determine whether the thread is the thread that created the UI. If it does not match, an exception will be thrown. Updating the UI will not make thread judgment when the UI is not displayed on the screen.

The setView method of ViewRootImpl:

In the setView method, the requestLayout () method is first called to determine the thread. If the thread matches, call scheduleTraversals () to complete the measurement, layout and drawing of View. The next step in setView is to remotely call the addToDisplay method in the IWindowSession object, and transfer the window and other information to NMS and call the addWindow method of NMS to complete the final display of window on the screen.

IWindowSession WindowManagerService

Here is the key to displaying the View on the screen, passing the View from the application process to the system process and then completing the display. The IWindowSession object in ViewRootImpl is obtained through the static method getWindowSession () of WindowManagerGlobal. In this method, firstly, the remote proxy of the system service WindowsManagerService in the application process is obtained by IPC through Binder through ServiceManager, and then the implementation class Session class remote proxy object of the IWindowSession interface is obtained by calling the openSession () method of WMS through AIDL communication. Session class is also a Binder, so it can be passed across processes.

In the addToDisplay method of the remote agent of Session, the addToDisplay method of Session is called by AIDL to pass the window information to the system process, then the addWindow method of AMS is called, and AMS finally displays the View in Window to the screen.

two。 Delete operation is to delete an existing Window on the screen

In the WindowManagerImpl.removeView operation, first find the View to be deleted through findViewLocked, then find the ViewRootImpl of the corresponding Window through View, delete View, LayoutParams, and ViewRootImpl from the corresponding ArrayList, and then call the remove of Session through IPC, which calls the removeWindow method of WMS, and remove the View corresponding to the Window on the screen.

3. Update operation

WindowManagerImpl.updateViewLayout, set the new LayoutParams for view, find the corresponding ViewRootImpl through findViewLocked, delete the old LayoutParams in the LayoutParams collection, add the new LayoutParams in the original location of the collection, call the setLayoutParams of ViewRootImpl to complete the re-measurement, layout and drawing of View, and finally call Session through IPC and then call WMS to complete the update of window.

4. Add Window code

The custom Window does not actively create the Window in the process of creation, but is maintained by the system when it is displayed. This also reflects that Window is an abstract concept, and it is View that needs to be dealt with finally.

Private void addWindow () {TextView view = new TextView (this); view.setText ("Text"); view.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Log.i ("renxl", "onClick");}}); view.setBackgroundColor (Color.RED) / / the View to be displayed can be newly created or WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams (WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, 0,0, PixelFormat.TRANSPARENT) obtained by LayoutInflater from the xml layout; / / it must be WindowManager.LayoutParams mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; / / choose one of the three flag, mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST / / type indicates priority mLayoutParams.gravity = Gravity.START | Gravity.TOP; mLayoutParams.x = 100; / / X-axis position mLayoutParams.y = 300 on the screen; / / Y-axis position on the screen WindowManager manager = (WindowManager) getSystemService (WINDOW_SERVICE); manager.addView (view, mLayoutParams); / / add View to the interface} * * Note: if the system-level Window has a priority of more than 1999, you need to declare permissions * *

Fifth, user touch screen event handling process

WMS passes the event IPC to Window,Window to call its internal CallBack object, that is, Activity or Dialog object or method. Finally, the event is passed to View, and then the response View and the corresponding window IPC are submitted to WMS through ViewRootImpl to complete the display after the response.

VI. Creation of common Window

1. Window creation process of Activity

In the Activity startup process, window is assigned a new PhoneWindow in Activity's attach method, and setContentView passes layout to PhoneWindow. PhoneWindow loads the layout View through LayoutInflater's inflate method and adds it to DecorView inside PhoneWindow. When onResume, call WindowManager's addView method to display DecorView in Window to the screen through WMS

When Activity creates the Window, it implements the methods in the Callback interface of Window. When the Window receives the touch, it will call back the methods in Callback to pass the event to Activity, the Activity will call the distribution method in PhoneWindow, and the method in PhoneWindow will call the method in DecordView, and finally pass the event to View.

The reason for guessing that events are passed from WMS to Window, then to Activity and then to Window is that developers can handle events in Activity, not necessarily to View

2. The Window creation process of Dialog

Like Activity, a PhoneWindow is created when the Dialog object is instantiated, and when the show method is called, the View is added to the screen by calling the addView method of WMS through AIDL.

3. Window creation process of Toast

Toast does not actively create Window in the process of creation, but the window of Toast is maintained by the system when it is displayed. This also reflects that Window is an abstract concept, and it is View that needs to be dealt with finally.

The work project of Toast needs the cooperation of three parts of TN NMS WMS. IN is also a Binder,NMS. Calling TN is remote access, and TN calling WMS is also remote calling.

NMS is NotificationManagerService

The working process of Toast is divided into two steps.

The first step is to change the current thread into a TN object of type Binder, remotely call the enquneue method in NMS to transfer the TN to NMS, NMS remotely call the show method of TN, the show method in TN runs in the currently applied Binder thread pool, switch the process to the main thread through Handler's post series methods, and then call the method in WMS through WindowManager to complete the show method of TN in the show process NMS. A message showing the time for Toast will be sent through its own internal Handler delay. After receiving the message, the Handler in NMS will call the hide method of TN (remote call procedure), and the hide method in TN will remotely call the hide method in WMS through WindowManager to hide the Toast. To complete the whole process.

VII. Summary

Each window displayed on the screen requires a combination of window and View, and there can be multiple Window on the screen. The View described below is the root View contained in a Window.

The creation of window and the addition, deletion and update of View are realized by WindowManager, and the operation of window in WindowManager will realize the current window operation on the screen by remotely requesting the method in IWindowSession through IPC in the corresponding ViewRootImpl of each window and then calling the corresponding method of WMS.

Each Window corresponds to a ViewRootImpl, and window manages the view through the corresponding ViewRootImpl.

When there is user interaction on the screen, WMS will pass the event to the corresponding interface. The Window,Window will call the corresponding CallBack of the current interface to handle the event.

WindowManager is the interface, and the implementation class is in WindowManagerImpl,WindowManagerImpl and completes the operation through WindowMAnagerGlobal. Typical bridging mode

Adding Window does not show the problem

Due to the domestic customization of ROM, many models prohibit the creation of suspension windows by default, so if it is not displayed, check whether the permission of the application is turned off.

Android 6.0has added the switch setting for permissions, and the floating window permission is turned off by default. Some domestic customized Rom 6.0s can set the switch of permission, and the floating window permission is off by default.

Problem solving

MLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST

Set type to TYPE_TOAST, there is no restriction on TYPE_TOAST in the source code.

On the domestic customized Rom, only a few models can display the listening events of View when setting TYPE_TOAST.

On how to understand the management of Window in Android is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report