In depth explanation of window management in Android

1、 Understand Android window

Window represents the concept of a window. It is an abstract concept. Each window corresponds to a view and a viewrootimpl. Window and view are connected through viewrootimpl. Therefore, 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 are initialized, they will create a corresponding phonewindow and assign it to an internal reference

Window hierarchy

Windowlayoutparams.settype settings

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 higher level will cover the lower level

The child window must depend on the parent window. For example, dialog must pop up in activity. The window in dialog is the child window and the window in activity is the parent window

Displaying system level windows requires permissions < uses permission Android: name = "Android. Permission. System_alert_window" / >

Flags of windowlayoutparams

FLAG_ NOT_ The foucsable window does not need to obtain focus or receive various input events. Flag is enabled at the same time_ NOT_ TOUCH_ MODAL FLAG_ NOT_ TOUCH_ The modal system will transfer the click events outside the current window area to the underlying window, and handle the events in the current window area by itself

FLAG_ SHOW_ WHEN_ Locked enable this mode to display the window on the lock screen interface

2、 Understanding WindowManager in Android

Windows in Android are managed through WindowManager. After creating phonewindow, WindowManager will be set for the window object. WindowManager is an interface that inherits the viewmanager interface. It can also be seen from here that the operation on window is actually the operation on view. The implementation class of WindowManager is windowmangerimpl, Windowmangerimpl is created through new.

3、 Association between window and windowmanagerimpl

Through ContextImpl's getSystemService, WindowManagerImpl instances can be obtained. The same ContextImpl gets the same WindowManagerImpl object. After WindowmangerImpl is obtained, Window's setWindowManager method is invoked to establish the relationship between Window and WindowManagerImpl.

Setwindowmanager is mainly used to re create a windowmanagerimpl bound to the current window based on the windowmanagerimpl instance, and assign a value to the attribute mwindowmanager in the window

That is, on the Java layer, window establishes the first contact with WindowManager, and assigns WindowManager in activity, dialog, etc. as a new windowmanagerimpl object.

Note: here, we use the singleton windowmanagerimpl, combine different windows, and finally build a non singleton windowmanagerimpl object associated with the window

4、 Operation on window

1. Add windowmanagerimpl.addview. Note that it is to add a new window, not to operate on the view in a window

Each window displayed in Android is actually the process of displaying the view to the screen. If we customize a layout to be displayed and get the view object, we just call the addview method of the windowmanagerimpl object. We can get the windowmanagerimpl instance 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 transferred to windowmanagerglobal. In the transfer process, in addition to passing the view and layoutparams, the associated window objects in windowmanagerimpl are also passed together

Addview method of windowmanagerglobal

Judge the validity of view, layoutparams and other parameters in windowmanagerglobal, create viewrootimpl, add viewrootimpl, view and layoutparams to the corresponding araylist set in windowmanagerglobal, and then call the setview method of viewrootimpl

ViewRootImpl

It inherits from the handler class and serves as a bridge for the communication between the native layer and the Java layer view system

When the viewrootimpl is created, the reference of the thread that created it is saved. When updating the view during the development process, it will judge whether the current thread is the thread that created the viewrootimpl. If not, an exception will be thrown.

Generally, the viewrootimpl is created in the main thread, so an exception will be thrown when the UI is updated in the child thread, 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. At this time, the UI needs to be updated through the viewrootimpl. The screen of the activity does not display before the onresume. Modifying the UI will only modify the UI in the layout, The method of viewrootimpl is not called and displayed on the screen.

Therefore, it is concluded that only after the UI is displayed on the screen, when updating the UI, it will judge whether the thread is the thread creating the UI. If it does not match, an exception will be thrown. When updating the UI is not displayed on the screen, it will not judge the thread

Setview method of viewrootimpl:

IWindowSession WindowManagerService

Here is the key to displaying the view on the screen. It is the place where the view is passed from the application process to the system process and then displayed. The iwindowsession object in viewrootimpl is obtained through the static method getwindowsession() of windowmanagerglobal. In this method, firstly, the IPC of the system service windowsmanagerservice in the application process is obtained through servicemanager through binder, and then the opensession() of WMS is called through Aidl communication Method to obtain the implementation class of iwindowsession interface. Session class is a remote proxy object. Session class is also a binder, so it can be passed across processes

In the addToDisplay method of Session's remote proxy, the window information is passed to the system process through AIDL's calling Session's addToDisplay method, then the addWindow method of AMS is invoked, and AMS finally displays the View in Window to the screen.

2. Delete is to delete an existing window on the screen

In the WindowManagerImpl.removeView operation, we first look for 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 ViewRootImpl method by calling the ViewRootImpl of the IPC. Remove the view corresponding to the window on the screen

3. Update operation

Windowmanagerimpl.updateviewlayout, set a new layoutparams for the view, find the corresponding viewrootimpl through findviewlocked, delete the old layoutparams in the layoutparams set, add a new layoutparams in the original position of the set, and call setlayoutparams of viewrootimpl to complete the re measurement, layout and drawing of the view, Finally, call session through IPC, and then call WMS to update the window.

4. Add window code

The user-defined window is not actively created during the creation process, but is maintained by the system during display. This also reflects that window is an abstract concept, and the final thing to deal with is view

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); // 要显示的 View 可以是新创建的,也可以是 LayoutInflater 从 xml 布局中获取的

  WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT,PixelFormat.TRANSPARENT); // 必须是 WindowManager.LayoutParams
  mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; // 三种 flag 从中选一
  mLayoutParams.type = WindowManager.LayoutParams.TYPE_TOAST; // type 表示优先级
  mLayoutParams.gravity = Gravity.START | Gravity.TOP;
  mLayoutParams.x = 100; // 在屏幕上 X 轴位置
  mLayoutParams.y = 300; // 在屏幕上 Y 轴位置

  WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
  manager.addView(view,mLayoutParams); // 将 View 添加到界面上
}

**注意,如果是系统级别的 Window 也就是优先级超过 1999 的,需要声明权限**
<uses-permission android:name="android.permission.SYstem_ALERT_WINDOW" />

5、 User touch screen event processing flow

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

6、 Common window creation

1. Window creation process of activity

In the activity startup process, a new phonewindow is assigned to the window in the attach method of the activity, and setcontentview passes the layout to the phonewindow. In the phonewindow, the layout view is loaded through the insert method of the layouteinflator and added to the decorview inside the phonewindow. When onresume, Call the addview method of WindowManager to display the decorview in window on the screen through WMS

When creating a window, the activity implements the methods in the callback interface of the window. When the window receives a touch, it will call back the methods in the callback to pass the event to the activity. The activity will call the distribution method in phonewindow, and the phonewindow will call the methods in decardview to finally pass the event to the view.

Guess that the reason why events are passed from WMS to window, then to activity, and then to window is that developers can process events in activity, not necessarily to view

2. Window creation process of dialog

The same as activity. When instantiating the dialog object, create phonewindow. When calling the show method, call the addview method of WMS through Aidl to add the view to the screen

3. Toast window creation process

Toast does not actively create a window in the creation process, but the system maintains toast's window during display. This also reflects that window is an abstract concept, and the final thing to deal with is view

The work project of Toast requires three parts of TN NMS WMS to work together. IN is also a Binder. In NMS, calling TN is remote access, and TN calling WMS is also called remote.

NMS is notificationmanagerservice

Toast's work process is divided into two steps

7、 Summary

Each window displayed on the screen requires the combination of window and view. There can be multiple windows on the screen. The following views are the root views contained in a window

The creation of window and the addition, deletion and update of view are realized by WindowManager. The operation of window in WindowManager is realized by remote requesting the method in iwindowsession through IPC in the viewrootimpl corresponding to each window, and then calling the corresponding method of WMS to realize the operation of current window on the screen.

Each window corresponds to a viewrootimpl. The window manages the view through the corresponding viewrootimpl

When there is user interaction on the screen, WMS will transfer the event to the window of the corresponding interface, and the window will call the corresponding callback of the current interface to process the event

WindowManager is an interface, and the implementation class is windowmanagerimpl. Windowmanagerimpl completes operations through windowmanagerglobal. Typical bridging mode

Adding window does not show the problem

Due to the customization of ROM in China, many models will prohibit the application from creating the floating window by default. Therefore, if it is not displayed, check whether the permission of the application is closed.

Problem solving

mLayoutParams.type = WindowManager.LayoutParams.TYPE_ TOAST;

Set type to type_ Toast, type in the source code_ Toast has no restrictions.

On the domestic customized ROM, only a few models will set type_ During toast, the listening events of view cannot be obtained, and the display is OK.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>