Android Judging Screen lock screen summary of steps

Source: Internet
Author: User

There are a total of two types of methods:

First, the code directly determine

Ii. Reception of broadcasts

Now let's start with the first class of methods (the code directly determines):

1, through the PowerManager Isscreenon method, the code is as follows:

PowerManager pm = (powermanager) context.getsystemservice (Context.power_service); Boolean isscreenon = Pm.isScreenOn () ;//If true, the screen is "lit", otherwise the screen is "dark".


Note has been written very clear, and now about to say,

The screen "lit" indicates that there are two states: A, unlocked screen B, is currently in the unlocked state. Both of these status screens are lit

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

2, through the Keyguardmanager Inkeyguardrestrictedinputmode method, the code is as follows:

Keyguardmanager Mkeyguardmanager = (keyguardmanager) context.getsystemservice (Context.keyguard_service); Boolean Flag = Mkeyguardmanager.inkeyguardrestrictedinputmode ();

The note has been written very clearly, and now probably say, Boolean flag = Mkeyguardmanager.inkeyguardrestrictedinputmode ();
The source return value is interpreted as: true if in Keyguard restricted input mode.
After testing, the conclusion is:
If flag is true, it indicates that there are two states: A, the screen is black B, and is currently in the unlocked state.
If flag is false, it indicates that the screen is not currently locked

Note: The above two methods can also be called through the reflection mechanism.
Here is an example of the first method.

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 introduce the second class method (the broadcast of the receiving system):

Receive System broadcast event, the screen in three states (open screen, lock screen, unlock) between the time of the transformation, the system will send 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 (); F (Intent.ACTION_SCREEN_ON.equals (action)) {//Open} else if (Intent.ACTION_SCREEN_OFF.equals (action)) {//Lock screen} else if (in Tent. 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);}


This article transferred from: http://blog.csdn.net/heroxuetao/article/details/24639203

Overview of how Android determines screen lock screen

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.