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