To create a project, you need to determine whether the screen is locked or not and find that there are many online methods, but they are complicated. Now let's summarize:
There are two methods in total:
I,Code Direct Determination
II,Receive Broadcast
Now let's talk about the first method (Code-based determination ):
1. Use the isScreenOn method of PowerManager. The Code is as follows:
PowerManager pm = (PowerManager) context. getSystemService (Context. POWER_SERVICE); boolean isScreenOn = pm. isScreenOn (); // if it is true, the screen is "bright"; otherwise, the screen is "dark.
The comment has been clearly written. Let's talk about it now,
The screen is highlighted, indicating that there are two statuses: a, no screen lock B, and is currently unlocked. The screens are both bright.
The screen is "dark", indicating that the current screen is black.
2. Use the inKeyguardRestrictedInputMode method of KeyguardManager. The Code is as follows:
KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);boolean flag = mKeyguardManager.inKeyguardRestrictedInputMode();
The comment has been clearly written. Now let's talk about it. boolean flag = mKeyguardManager. inKeyguardRestrictedInputMode ();
The Return Value of the source code is interpreted as true if in keyguard restricted input mode.
After the test, the summary is:
If the flag is true, there are two statuses: a. The screen is black. B. The screen is currently unlocked.
If the flag is false, the screen is not locked currently.
Note: The two methods above can also be called through the reflection mechanism.
The first method is used as an example.
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()}
Now we will introduce the second method (Receiving System Broadcasts ):
When a system broadcast event is received and the screen changes between three statuses (on-screen, screen lock, and unlock), the system sends broadcasts. We only need to listen to these broadcasts.
The Code is as follows:
Private ScreenBroadcastReceiver mScreenReceiver; private class ScreenBroadcastReceiver extends BroadcastReceiver {private String action = null; @ Overridepublic 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) {// screen lock} 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 );}
Well, I have written so much and hope it will be useful to everyone !!