1. Two options
(1) Monitoring Broadcastreceiver
(2) Get PowerManager Event
2. Broadcastreceiver
2.1. Events
There are a total of 3 events:
Intent. action_screen_on : Screen lit intent. action_screen_off : Screen off intent. action_user_present : User Unlocked
2.2. Permissions
Listener user unlocking requires permission to be registered in Androidmanifest
<android: Name/>
2.3. Intentfilter Code
if (mcontext! = null) { Intentfilter filter = new Intentfilter (); Filter.addaction (intent.action_screen_on); Filter.addaction (Intent.action_screen_off); Filter.addaction (intent.action_user_present); Mcontext.registerreceiver (mscreenreceiver, filter); }
3. PowerManager
PowerManager manager = (PowerManager) mcontext.getsystemservice (context.power_service); if (Manager.isscreenon ()) { if (mscreenstatelistener! = null) { mscreenstatelistener.onscreenon (); } } else { if (mscreenstatelistener! = null) { mscreenstatelistener.onscreenoff (); } }
4. Code
Import Android.annotation.suppresslint;import Android.content.broadcastreceiver;import android.content.Context; Import Android.content.intent;import android.content.intentfilter;import android.os.powermanager;/** * Created by Xiyanglu on 4/11/15. */public class Screenobserver {private Context mcontext; Private Screenbroadcastreceiver Mscreenreceiver; Private Screenstatelistener Mscreenstatelistener; Public Screenobserver (Context context) {Mcontext = context; Mscreenreceiver = new Screenbroadcastreceiver (); } public void Startobserver (Screenstatelistener listener) {Mscreenstatelistener = listener; Registerlistener (); Getscreenstate (); } public void Shutdownobserver () {Unregisterlistener (); }/** * Gets the screen status */@SuppressLint ("Newapi") private void Getscreenstate () {if (Mcontext = = NULL ) {return; } PowerManager Manager = (PowerManager) mcontext.getsystemservice (ConteXt. Power_service); if (Manager.isscreenon ()) {if (Mscreenstatelistener! = null) {Mscreenstatelistener.onscreenon ( ); }} else {if (Mscreenstatelistener! = null) {Mscreenstatelistener.onscreenoff (); }}} private void Registerlistener () {if (Mcontext! = null) {Intentfilter filter = New Intentfilter (); Filter.addaction (intent.action_screen_on); Filter.addaction (Intent.action_screen_off); Filter.addaction (intent.action_user_present); Mcontext.registerreceiver (mscreenreceiver, filter); }} private void Unregisterlistener () {if (Mcontext! = null) Mcontext.unregisterreceiver (MSCREENR Eceiver); } 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 Mscreenstatelistener.onscreenon (); } else if (Intent.ACTION_SCREEN_OFF.equals (ACTION)) {//Lock screen Mscreenstatelistener.onscreenoff (); } else if (Intent.ACTION_USER_PRESENT.equals (ACTION)) {//unlock mscreenstatelistener.onuserpresent (); }}} public interface Screenstatelistener {//Return to caller screen state information public void Onscreenon (); public void Onscreenoff (); public void onuserpresent (); }}
5. References
[1] Android monitor screen unlock event http://blog.csdn.net/gf771115/article/details/7991892
Android Monitor lock screen/Open screen event