How does Android realize pop-up window in lock screen state

preface

The first idea to realize pop-up on the lock screen is to use @ H_ 301_ 5@WindowManager Set @ h_ 301_ 5@Window @ h_ 301_ 5@Flag , by setting @ h_ 301_ 5@Flag To display the window on the top of the lock screen.

Next is the @ h that may be relevant to the test_ 301_ 5@Window Type attribute to verify whether the scheme is feasible.

You need to specify the permissions required for each window type before trying the window type attribute. The following is @ H_ 301_ 5@com.android.internal.policy.impl.PhoneWindowManager.checkAddPermission Source code:

Obviously unsuitable @ h_ 301_ 5@Type :@H_ 301_ 5@TYPE_TOAST ,@H_ 301_ 5@TYPE_INPUT_METHOD ,@H_ 301_ 5@TYPE_WALLPAPER ; Possible @ h_ 301_ 5@Type :@H_ 301_ 5@TYPE_PHONE ,@H_ 301_ 5@TYPE_PRIORITY_PHONE ,@H_ 301_ 5@TYPE_SYstem_ALERT ,@H_ 301_ 5@TYPE_SYstem_ERROR ,@H_ 301_ 5@TYPE_SYstem_OVERLAY ; Other types of @ h_ 301_ 5@Type :

System signature permission required:

The system signature is required to apply for the permission, so we cannot obtain the permission.

TYPE_ PHONE

@H_ 301_ 5@TYPE_PHONE Type windows can be displayed on other apps, but not on the lock screen, so pass.

TYPE_ PRIORITY_ PHONE

@H_ 301_ 5@TYPE_PRIORITY_PHONE Type windows can be displayed on other apps, but not on the lock screen, so pass. Moreover, the actual behavior is inconsistent with the comments. This type of window can obtain interactive events. The specific reason remains to be found.

TYPE_ SYstem_ ALERT

@H_ 301_ 5@TYPE_SYstem_ALERT Type windows can be displayed on other apps, but not on the lock screen, so pass.

TYPE_ SYstem_ OVERLAY

@H_ 301_ 5@TYPE_SYstem_OVERLAY Type of window can be displayed on all other windows, including the lock screen, and will not affect the interactive event response of the window below it. However, the attribute window cannot obtain the focus and cannot interact (if the window can obtain the focus, it can be used to grab the user's lock screen password. For security reasons, the system will not allow it), Therefore, it can only be used to simply display content. If an interactive lock screen pop-up window is required, the attribute pass.

TYPE_ SYstem_ ERROR

The test can be displayed under the native ROM 5.1, but according to the notes (appear on top of everything they can) can not be displayed on the lock screen in all cases, and ROMs such as MIUI and flyme mask the floating window permission by default. In view of this, use @ H_ 301_ 5@WindowManager Add floating window to realize the basic pass of the scheme of locking screen pop-up window.

Implemented using activity

First, you need to set the activity as follows

One of the most important and necessary settings is: @ h_ 301_ 5@FLAG_SHOW_WHEN_LOCKED As the name implies, this @ h is displayed on the lock screen_ 301_ 5@Activity 。 And the other @ h_ 301_ 5@Flag Including unlocking, keeping the screen always on and lighting the screen. You can select settings according to specific needs.

At androidmanifest Declare activity in XML

The same @ h_ 301_ 5@Activity Also need to be in @ h_ 301_ 5@AndroidManifest.xml You should add @ h when declaring_ 301_ 5@android : excludefromcontents = "true" attribute is used to set the @ h_ 301_ 5@Activity Remove from the recent task list, otherwise users will find it strange. And because of this @ h_ 301_ 5@Activity The whole lock screen will be covered on the lock screen, and even if the background is set to be transparent, the lock screen interface will not be displayed below (the system is mainly for security reasons), so it is necessary to consider this @ H_ 301_ 5@Activity In order to show the background, the theme is set as wallpaper.

Start activity

Due to this @ h_ 301_ 5@Activity In order to display in the case of lock screen, start @ H_ 301_ 5@Activity Don't forget to judge whether the mobile phone is in the lock screen state. You can judge the lock screen state in the following way:

summary

The above is all about realizing the pop-up effect in the locked screen state in Android. I hope the content of this article can be helpful to you when developing Android. If you have any questions, you are welcome to leave a message for discussion.

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