Detailed explanation of the correct posture of Android screen lock pop-up window demo

In the last article, I introduced the function of pop-up window under the screen of imitation QQ lock for Android program development. Today, I will share with you the correct posture of Android lock screen pop-up.

Recently, I was working on a function of screen lock screen and floating window, so I searched a lot of relevant information about Android screen lock screen on the Internet. In view of the fragmented information on the Internet, I summarize it here. This article will analyze the screen lock from the following two points:

1. How to monitor system screen lock

2. How to pop up a floating window in the lock screen interface

How to monitor system screen lock

After summary, the lock screen of the monitoring system can be locked in the following two ways:

1) Code direct determination

2) Receive broadcast

1) Code direct determination

There are also two methods to judge the code:

a) Through the isscreen on method of PowerManager, the code is as follows:

Here we need to explain:

The screen "on" indicates that there are two states: A. unlocked screen B. It is currently unlocked. Both status screens are on;

The screen is "dark", indicating that the current screen is black.

b) Through the keyguardrestrictedinput mode method of keyguardmanager, the code is as follows:

The following description is given for the flag. After the test, it is summarized as follows:

If the flag is true, there are two states: a. the screen is black; B. the screen is currently locked.

If the flag is false, it means that the screen is not locked at present

Note: the above two methods can also be called through the reflection mechanism.

The reflection code is as follows:

2) Receive broadcast

When the Android system lock screen or screen lights up or the screen is unlocked, the system will send corresponding broadcasts. We only need to monitor the broadcasts. The pseudo code for registering broadcasts is as follows:

How to pop up a floating window in the lock screen interface

I know how to monitor the system screen, so the next step is to pop up the suspension box when the screen is locked. There are two ways to implement this:

1) Using WindowManager

2) Use activity

The current situation is that these two methods can be realized on the real machine. If netizens find problems, they can leave messages in the blog

1) Using WindowManager

The code is as follows:

The main configuration of WindowManager is the above codes. It should be explained here that the type of type has the following values:

If you want the suspended window to be above the lock screen, use type_ SYstem_ Error, because it appears on top of everything.

2) Use activity

Activity settings

The following settings are required for activity to pop up in lock screen status. First, the oncreate method. You need to add four flags, as follows:

As the name suggests, the four flag bits are displayed in the locked screen state, unlocked, keep the screen bright and open the screen. In this way, when the activity is started, it will be unlocked and displayed on the screen. Then at androidmanifest In the XML file, the following attributes need to be added to the declaration of the activity:

For layout files, the view to be displayed is centered and the background is transparent. Since the background has been set as the background of wallpaper, the background of desktop is displayed. If the background is set to the default white, the pop-up window will be white and look ugly. If the background is set to transparent, the unlocked interface will be displayed behind the pop-up window (even if there is a lock screen password, the unlocked interface will also be displayed), which also affects the visual effect.

Activate lock screen pop-up in broadcast

We set it to pop up only under the lock screen. It is not suitable to pop up this window under the non lock screen (you can try, the effect will be very strange). Generally, a broadcast receiver is registered to determine whether a pop-up window is required after receiving the specified broadcast. Therefore, in the receiving code of broadcastreceiver, it is necessary to first determine whether it is in the locked screen state:

The keyguardmanager class used here is used to manage the lock screen. After 4.1, the API of this class added an iskeyguardlocked() method to determine whether to lock the screen. However, before 4.1, we can only use the inkeyguardrestricedinputmode() method. If it is true, it is the lock screen state. It should be noted that the context for starting an activity in the broadcast may not be an activity object, so new needs to be added_ Task flag, otherwise an error may be reported during startup. We can pop up the corresponding suspension window after sending the broadcast in combination with the previous system.

Copy onnewintent method

Light up the screen again. If the activity does not exit, but the lock screen key is pressed manually, the screen will not be aroused when the previous broadcast receiver starts it again. Therefore, we need to add the code to wake up the screen in the activity. Here, the power source lock is used. It can be added to onnewintent (intent intent) because it will be called. You can also add other appropriate lifecycle methods. Add the following code:

Finally, add the following permissions

The first is to unlock the screen, and the second is to apply for power lock.

The above is a detailed explanation of the correct posture demo of the Android screen lock pop-up window introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message. Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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