Preface
If you want to implement pop-up windows on the lock screen, the first idea is to use them.WindowManager
set upWindow
ofFlag
, by settingFlag
The display priority is to make the window appear on the lock screen.
Next is what the experiment might be relevantWindow Type
Attributes, verify that the scheme is feasible.
Before trying each Window Type property, you need to clarify the permissions required by each Type. Here isSource code:
public int checkAddPermission( attrs) { int type = ; if (type < .FIRST_SYSTEM_WINDOW || type > .LAST_SYSTEM_WINDOW) { return WindowManagerImpl.ADD_OKAY; } String permission = null; switch (type) { case TYPE_TOAST: // XXX right now the app process has complete control over // this... should introduce a token to let the system // monitor/control what they are doing. break; case TYPE_INPUT_METHOD: case TYPE_WALLPAPER: // The window manager will check these. break; case TYPE_PHONE: case TYPE_PRIORITY_PHONE: case TYPE_SYSTEM_ALERT: case TYPE_SYSTEM_ERROR: case TYPE_SYSTEM_OVERLAY: permission = .SYSTEM_ALERT_WINDOW; break; default: permission = .INTERNAL_SYSTEM_WINDOW; } if (permission != null) { if ((permission) != PackageManager.PERMISSION_GRANTED) { return WindowManagerImpl.ADD_PERMISSION_DENIED; } } return WindowManagerImpl.ADD_OKAY; }
Obviously unsuitableType
:TYPE_TOAST
, TYPE_INPUT_METHOD
, TYPE_WALLPAPER
; Possible suitableType
:TYPE_PHONE
, TYPE_PRIORITY_PHONE
, TYPE_SYSTEM_ALERT
, TYPE_SYSTEM_ERROR
, TYPE_SYSTEM_OVERLAY
; Other types ofType
:
System signature permission is required:
.INTERNAL_SYSTEM_WINDOW
Applying for this permission requires system signature, so we cannot obtain permission.
TYPE_PHONE
/** * Window type: phone. These are non-application windows providing * user interaction with the phone (in particular incoming calls). * These windows are normally placed above all applications, but behind * the status bar. * In multiuser systems shows on all users' windows. */ public static final int TYPE_PHONE = FIRST_SYSTEM_WINDOW+2;
TYPE_PHONE
A type of window can be displayed on top of other APPs, but cannot be displayed on top of the lock screen, so PASS.
TYPE_PRIORITY_PHONE
/** * Window type: priority phone UI, which needs to be displayed even if * the keyguard is active. These windows must not take input * focus, or they will interfere with the keyguard. * In multiuser systems shows on all users' windows. */ public static final int TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW+7;
TYPE_PRIORITY_PHONE
A type of window can be displayed on top of other APPs, but cannot be displayed on top of the lock screen, so PASS. Moreover, the actual behavior does not match the comments. This type of window can obtain interactive events, and the specific reasons are to be found.
TYPE_SYSTEM_ALERT
/** * Window type: system window, such as low power alert. These windows * are always on top of application windows. * In multiuser systems shows only on the owning user's window. */ public static final int TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW+3;
TYPE_SYSTEM_ALERT
A type of window can be displayed on top of other APPs, but cannot be displayed on top of the lock screen, so PASS.
TYPE_SYSTEM_OVERLAY
/** * Window type: system overlay windows, which need to be displayed * on top of everything else. These windows must not take input * focus, or they will interfere with the keyguard. * In multiuser systems shows only on the owning user's window. */ public static final int TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW+6;
TYPE_SYSTEM_OVERLAY
A window of type can be displayed on top of all other windows, including the lock screen, and will not affect the interaction event response of the window below it. However, this property window cannot obtain focus and cannot interact (if the window can obtain focus, it can be used to grab the user's lock screen password. For security reasons, the system will not allow it), so it can only be used to simply display content. If an interactive lock screen popup window is required, then this property is PASS.
TYPE_SYSTEM_ERROR
/** * Window type: internal system error windows, appear on top of * everything they can. * In multiuser systems shows only on the owning user's window. */ public static final int TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW+10;
The experiment can be displayed under native ROM 5.1, but according to the comments (appear on top of everything they can) not be displayed on the lock screen in all cases, and ROMs such as MIUI and Flyme are blocked by default for floating window permissions. Considering this, useWindowManager
The basic solution of adding floating windows is to implement the lock screen pop-up window.
Implementation using Activity
First, you need to set the Activity as follows
protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); final Window win = getWindow(); (.FLAG_SHOW_WHEN_LOCKED | .FLAG_DISMISS_KEYGUARD | .FLAG_KEEP_SCREEN_ON | .FLAG_TURN_SCREEN_ON); }
The most important thing that must be set is:FLAG_SHOW_WHEN_LOCKED
, as the name implies, it is to display it under the lock screenActivity
. And the othersFlag
Including: unlocking, keeping the screen always on, and lighting up the screen can be selected according to specific needs.
Declare Activity in
It's the sameActivity
Need to be in
Declaration in the statement, please pay attention to adding it when declaringandroid:excludeFromRecents="true"
The attribute is to make theActivity
Remove from the recent task list, otherwise the user will find it strange. And because of thisActivity
The entire lock screen will be covered, and even if it is set to be transparent in the background, the lock screen interface will not be displayed below (the system is mainly for safety reasons), so you need to consider thisActivity
The background, so that the theme is not too abrupt here to display it.
<activity android:name=".LockScreenActivity" android:launchMode="singleInstance" android:excludeFromRecents="true" android:theme="@android:style/"/>
Start Activity
Because ofActivity
It is for displaying it in the lock screen, so it is activatedActivity
Don’t forget to determine whether the phone is in the lock screen state. You can judge the lock screen state in the following way:
KeyguardManager km = (KeyguardManager) (Context.KEYGUARD_SERVICE); if (()) { // In lock screen}
Summarize
The above is all about implementing the pop-up effect in the lock screen state in Android. I hope the content of this article will be helpful to everyone when developing Android. If you have any questions, please leave a message to discuss.