In the previous article to introduce the Android program to develop imitation of the new version of QQ lock screen under the window function. Today we share the correct posture of Android lock screen window.
Recently in doing a screen lock screen suspension window function, so on the internet to search a lot of Android screen lock the relevant information, in view of the online data is relatively fragmented, so I am here to summarize. This article resolves screen lock screens from the following two points:
1. How to monitor the system screen lock screen
2. How to eject the suspension window in the lock screen interface
How to monitor system screen lock screen
After summing up, the monitoring system's lock screen can be used in the following two ways:
1) Direct Code determination
2) receive the broadcast
1) Direct Code determination
There are two ways to judge your code:
A) through the PowerManager Isscreenon method, the code is as follows:
PowerManager pm = (powermanager)
context.getsystemservice (context.power_service);
If true, the screen is "lit" or the screen is "dimmed".
Boolean isscreenon = Pm.isscreenon ();
Here's what you need to explain:
The screen is "on", which indicates two states: A, unlocked screen B, and is currently in the unlocked state. Both of these state screens are lit;
The screen "Dark" indicates that the screen is currently black.
b through the Keyguardmanager Inkeyguardrestrictedinputmode method, the code is as follows:
Keyguardmanager Mkeyguardmanager = (keyguardmanager) context.getsystemservice (context.keyguard_service);
Boolean flag = Mkeyguardmanager.inkeyguardrestrictedinputmode ();
A description of the flag, after the test, summed up as:
If flag is true, there are two states: A, the screen is black B, and is currently in a lock screen state.
If flag is false, it means that the screen is not currently locked
Note: The above two methods can also be invoked through the reflection mechanism.
The reflection code is as follows:
private static method mreflectscreenstate;
try {
mreflectscreenstate = PowerManager.class.getMethod (Isscreenon, new class[] {});
PowerManager pm = (powermanager) context.getsystemservice (activity.power_service);
Boolean isscreenon= (Boolean) Mreflectscreenstate.invoke (PM);
catch (Exception e) {
e.printstacktrace ()
}
2) receive the broadcast
When the Android lock screen or screen is lit, or the screen is unlocked, the system will send a corresponding broadcast, we only need to monitor the broadcast
The pseudo code for registering broadcasts is as follows:
Private Screenbroadcastreceiver mscreenreceiver;
Private class Screenbroadcastreceiver extends Broadcastreceiver {
private String action = null;
@Override public
void OnReceive (context context, Intent Intent) {
action = intent.getaction ();
if (Intent.ACTION_SCREEN_ON.equals (action)) {
//Open screen
} else if (Intent.ACTION_SCREEN_OFF.equals (action)) {
//Lock Screen
} else if (Intent.ACTION_USER_PRESENT.equals (ACTION)) {
//Unlock
}}}
private void Startscreenbroadcastreceiver () {
intentfilter filter = new Intentfilter ();
Filter.addaction (intent.action_screen_on);
Filter.addaction (Intent.action_screen_off);
Filter.addaction (intent.action_user_present);
Context.registerreceiver (mscreenreceiver, filter);
How to eject a suspension window in the screen lock
Actually know for the system screen monitoring method, then the next is to screen lock in the time, pop-up suspension box, the implementation of the way there are two kinds:
1) using WindowManager
2) Use of activity
At present, the use of these two methods in the real machine can be achieved, if users find that there is a problem, you can leave a message in the blog
1) using WindowManager
The code is as follows:
private void init (context mcontext) {this.mcontext = Mcontext; Mwindowmanager = (WindowManager) Mcontext.getsystemservic
E (Context.window_service);
Update floating window Position parameters pull over displaymetrics dm = new Displaymetrics ();
Get screen Information Mwindowmanager.getdefaultdisplay (). Getmetrics (DM);
Mscreenwidth = Dm.widthpixels;
Mscreenheight = Dm.heightpixels;
This.mwmparams = new Windowmanager.layoutparams (); Sets the window type if (Build.VERSION.SDK_INT >= build.version_codes). KitKat) {mwmparams.type = WindowManager.LayoutParams.TYPE_TOAST;} else {mwmparams.type =
WindowManager.LayoutParams.TYPE_PHONE;
//Set Picture format, the effect is transparent Mwmparams.format = pixelformat.rgba_8888;
Set the floating window not to be focused (to implement operations other than the visible window outside the floating window) mwmparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Adjust the docking position displayed by the suspension window for left-side placement? mwmparams.gravity = Gravity.left |
Gravity.top;
Mscreenheight = Mwindowmanager.getdefaultdisplay (). GetHeight ();
In the upper left corner of the screen as the origin, set the x, y initial values, relative to gravity mwmparams.x = 0;
Mwmparams.y = MSCREENHEIGHT/2; Set the suspension window long width data mwmparams.width = LayoUtparams.wrap_content;
Mwmparams.height = layoutparams.wrap_content;
AddView (CreateView (Mcontext));
Mwindowmanager.addview (this, mwmparams);
Mtimer = new Timer ();
Hide (); }
The main configuration of the
WindowManager is the above code, and here's how the type has the following values:
The application window.
public static final int first_application_window = 1;
The base window for all program windows, and other application windows are displayed on it.
public static final int type_base_application = 1; General application Function Program window.
Token must be set to the token of the activity to indicate who the window belongs to.
public static final int type_application = 2; The window to display when the application starts.
The application itself does not use this type.
It is used to let the system display some information until the application can open its own window.
public static final int type_application_starting = 3;
End of application window.
public static final int last_application_window = 99; child window.
The z-order and coordinate space of the child windows are dependent on their host window.
public static final int first_sub_window = 1000;
panel window, displayed at the top of the host window.
public static final int type_application_panel = First_sub_window; Media windows, such as video.
Appears below the host window.
public static final int type_application_media = first_sub_window+1; The child panel of the application window. Appears at the top of all panel windows.
(The general law of the GUI, the more "son" more on) public static final int type_application_sub_panel = First_sub_window +2; dialog box.
Similar to a panel window, drawing resembles a top-level window rather than a child window of a host.
public static final int type_application_attached_dialog = First_sub_window +3; Media information. Displayed between the media layer and the program window, you need to implement a transparent (translucent) effect. (for example, display subtitles) public static final int type_application_media_overlay = FIRSt_sub_window +4; child window ends.
(end of types of sub-windows) public static final int last_sub_window = 1999; System window.
Non-application creation.
public static final int first_system_window = 2000; Status bar.
There can be only one status bar, which is at the top of the screen, and other windows are located below it.
public static final int type_status_bar = First_system_window; Search bar.
There can only be one search bar, which is located above the screen.
public static final int type_search_bar = first_system_window+1; Phone window. It is used for telephone interaction (especially inbound).
It is placed above all applications, under the status bar.
public static final int type_phone = first_system_window+2; System prompts.
It always appears on top of the application window.
public static final int type_system_alert = First_system_window +3;
The Lock screen window.
public static final int type_keyguard = First_system_window +4; Information window.
Used to display toast.
public static final int type_toast = First_system_window +5; System top-level window. Show on top of everything else.
This window cannot get input focus or affect the lock screen.
public static final int type_system_overlay = First_system_window +6; Telephone priority, display when the screen is locked.
This window cannot get input focus or affect the lock screen.
public static final int type_priority_phone = First_system_window +7; System dialog box.
(For example, the Volume control box).
public static final int type_system_dialog = First_system_window +8;
The dialog box that appears when the screen is locked. PublIC static final int type_keyguard_dialog = First_system_window +9;
System internal error prompt, displayed on top of all content.
public static final int type_system_error = First_system_window +10; Internal Input Method window, displayed on top of the normal UI.
The application can be redesigned to avoid being overwritten by this window.
public static final int type_input_method = First_system_window +11;
Internal Input Method dialog box, displayed on top of the current Input Method window.
public static final int type_input_method_dialog= First_system_window +12;
Wallpaper window.
public static final int type_wallpaper = First_system_window +13;
The slide panel of the status bar.
public static final int type_status_bar_panel = First_system_window +14;
The system window ends. public static final int last_system_window = 2999;
If you want the suspension window to be on top of the lock screen, use type_system_error because it is displayed above all content.
2) Use of activity
The setting of the activity
The activity requires the following settings before the window can be played in the lock screen state.
The first is the OnCreate method, which requires the addition of 4 flags, as follows:
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Final Window win = GetWindow ();
Win.addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Your own code
The four sign bits as the name implies, is the lock screen state display, unlock, keep the screen long bright, open the screen. So when the activity starts, it unlocks and displays the screen.
Then, in the Androidmanifest.xml file, the declaration of the activity requires the following attributes to be added:
<activity android:name= ". Alarm. Alarmhandleractivity "
android:launchmode=" singleinstance "
android:excludefromrecents=" true "
Android:taskaffinity= ""
For layout files, the view is centered and the background is transparent. The background of the desktop is displayed because the background is set to the background of the wallpaper. If the background is set to the default white, it causes the window to be white behind it and looks ugly. If the background is set to transparent, then the window will display after the unlocked interface (even if there is a lock screen password, it will show the interface after the unlock), the same impact on the visual effects.
Start the Lock screen window in the broadcast
We set the lock screen only to play the window, not under the lock screen is not suitable for the pop-up window (you can try it, the effect will be very strange). It is generally registered to register a broadcast receiver, after receiving the specified broadcast to determine whether the need for the window, so in the Broadcastreceiver receive code to determine whether it is a lock screen state:
@Override public
void OnReceive (context context, Intent Intent) {
log.d (Log_tag, Intent.getaction ());
Keyguardmanager km = (keyguardmanager) context.getsystemservice (context.keyguard_service);
if (Km.inkeyguardrestrictedinputmode ()) {
Intent alarmintent = new Intent (context, alarmactivity.class);
Alarmintent.addflags (intent.flag_activity_new_task);
Context.startactivity (alarmintent);
}
Here is the Keyguardmanager class used to manage the lock screen, after 4.1 The API for this class adds a iskeyguardlocked () method to determine whether to lock the screen, But before 4.1, we can only use the Inkeyguardrestrictedinputmode () method, if true, that is the lock screen state. It should be noted that the context in which the activity is initiated in the broadcast may not be an activity object, so you need to add a new_task flag, otherwise you may be able to make an error at startup. We can combine the previous system to send the broadcast after the corresponding suspension window of the pop-up processing.
Replication Onnewintent Method
When the screen is lit again, if the activity does not exit, but is manually pressed by the lock screen, the screen will not be aroused when the front broadcast receiver starts it again, so we need to add the Wake screen code to the activity, which uses a power lock. Can be added in Onnewintent (Intent Intent) as it will be invoked. can also be added in other appropriate lifecycle methods. Add the following code:
PowerManager pm = (powermanager) this.getsystemservice (context.power_service);
if (!pm.isscreenon ()) {
Powermanager.wakelock wl = pm.newwakelock (Powermanager.acquire_causes_wakeup |
Powermanager.screen_bright_wake_lock, "BRIGHT");
Wl.acquire ();
Wl.release ();
Finally, add the following permissions
<uses-permission android:name= "Android.permission.DISABLE_KEYGUARD"/>
The first one is needed to unlock the screen, and the second one is required for a power lock.
The above mentioned is small series to introduce the Android Screen lock window of the correct posture demo, I hope to help you, if you have any questions welcome to my message, small series will promptly reply to everyone, here also thank you for the cloud Habitat Community support!