Android screen lock assessment method summary

Source: Internet
Author: User

 

 

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 !!

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.