Implementation of Android global floating window independent of activity

preface

When we use 360 security guard on the mobile phone, a small floating window will appear on the mobile phone screen all the time. Click the floating window to jump to the operation interface of the security guard, and the floating window is still visible without being covered by other activities (Duomi music also has relevant floating windows interacting with the main interface). So how is this floating window not affected by the activity interface implemented?

Android suspended window implementation

Implementation basis

Android floating window implementation uses WindowManager. WindowManager is introduced through context Getsystemservice (context. Window_service) can obtain the WindowManager object.

Each WindowManager object is bound to a specific display.

To obtain the WindowManager of a different display, you can use createdisplaycontext (display) to obtain the context of that display, and then use: context Getsystemservice (context. Window_service) to obtain WindowManager.

Using WindowManager, you can display windows on the top layer of other applications or even on the top layer of the mobile desktop.

The addview method and removeview method inherited from the base class of WindowManager are called to show and hide the window. See the following examples for details.

In addition, API 17 introduces presentation, which will automatically obtain the context and WindowManager of display, and can easily display windows on another display.

WindowManager needs to declare permissions to implement suspended windows

First, add the following permissions in the manifest:

Note: on MIUI, you need to turn on the "display floating window" switch of the application in settings and restart the application, otherwise the floating window can only be displayed in the application interface, not on the mobile phone desktop.

Service acquisition and basic parameter setting

Click and key events

In addition to the click events of various controls in the view, the disappearance control of pop-up view needs some processing.

Click outside the pop-up window to hide the pop-up window effect. First, the floating window is full screen, but the outermost layer is transparent or translucent:

Concrete implementation

summary

The above is the whole content of this paper. This paper analyzes the principle and specific implementation skills of Android floating window in detail in the form of examples, which has certain reference value and hopes to help you in developing Android applications.

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
分享
二维码
< <上一篇
下一篇>>