WindowManger,

update

How to add a View without using an Activity

The overall plan

The UI is displayed in the Service by adding a View through a Windows manager

The business scenario

The scenario

  • IQOO mobile phone, game assist

Can this scenario be done using the Activity mode

Using an activity blocks the underlying window, whereas adding a View using WindowManager does not

WindowManger profile

Windows Manager is an interface that ordinary App processes use to communicate with System services. To get a WindowManger instance object, use context.getSystemService (context.window_service)

How to use

  1. Add permissions
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Copy the code
  1. Manual boot enablement is displayed on top of other applications

  2. Get a WindowManger and add a View

WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); Windowmanager.addview (myView, p);Copy the code
  1. Remove View when not in use

WindowManager usage details

WindowManger

  1. Add the View:addView()

    Note that only one View can be added to a window. Adding multiple views raises the following exception

Caused by: java.lang.IllegalStateException: View android.widget.RelativeLayout{b3a2c6a V.E...... . I. 0, 0, 0, 0#7f070081 app:id/rl_root} has already been added to the window manager.
Copy the code
  1. Remove the View:remoview()

Since only one View can be added to a View, in order to add a View to an added View Window, you must remove the added View first

  1. Update layout properties:updateViewLayout(view,params)

Through to the second parameter WindowManger. LayoutParams attribute set, call this method just can make these properties take effect

WindowManger.LayoutParms

WindowManger.LayoutParms is actually a subclass of ViewGrop.LayoutParms, so the View can set LayoutParams directly

  1. Controls whether touch events can be responded to

    layoutParams.type

    • FLAG_NOT_TOUCHABLE
    • FLAG_NOT_TOUCH_MODAL
  2. Controls the display hierarchy of the window

    layoutParams.type

    • TYPE_TOAST(Android 5.0 and below can avoid permission issues)
    • TYPE_APPLICATION_WINOW(Application Layer Winow level)
    • TYPE_PRIORTY_PHONE(System layer Window level)
  3. Controls the display range of the view

    layoutParams.flags

    • FLAG_FULLSCREEN
    • FLAG_LAYOUT_IN_SCREEN
  4. Controls the display position of the view
    • layoutParams.width
    • layoutParams.height

The sample project

Github.com/happyburgla…

What other problems should we pay attention to?

  1. Use separate processes to avoid affecting the main business functions
  2. Upgrade the process priority to avoid being killed
    • Use the Front Desk service
    • 1 pixel keeps alive
    • Binding system Services

    Tip: You can run the oom_adj command to view the process level of a process

  3. The restart logic after the service is killed is processed

Source analyses

Due to the time, here we only have a brief introduction to the relevant source code, first look at the relevant classes

The WindowManger class, in fact, is just an interface to communicate with the common application process and system service WindowMangerservice

GetSystemServer (context.window_serice) to get an instance object of the WindowManger class

We can also see from the UML class diagram above that WindowManger is actually an interface, and its real instance object is the WindowMangerImpl class

WindowMangerImpl encapsulates the job of adding views into Windows ManagerGlobal. This class is also involved in another important class, Display, which encapsulates Display information. For example, Windows has logical and physical displays.

What are Logical displays

The source code explains that the logical display does not necessarily represent a specific physical display device, such as a built-in screen or an external display. Logical content The display can be displayed on one or more physical displays depending on the device type

What are physical displays

The actual Physical Displays on your Android device — say, your phone has 4K, but the manufacturer is trying to do something about it to save power. You can set the Logical displays to 1920 by 1080

Solemnly declare

The copyright of this article belongs to Android Institute. It is prohibited to be reproduced without permission.