1, first defining Screenlistener
Package com.app.lib;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.PowerManager;
/** * Created by ${zyj} on 2016/6/21. * * Public class Screenlistener {private context mcontext; private Screenbroadcastreceiver mscreenreceiver;
Statelistener Mscreenstatelistener; Public Screenlistener {mcontext = context; mscreenreceiver = new Screenbroadcastreceiver ();}/** * Scre En state broadcast receiver/private class Screenbroadcastreceiver extends Broadcastreceiver {private String action = null; @Override pub LIC void OnReceive (context context, Intent Intent) {action = Intent.getaction (); if (Intent.ACTION_SCREEN_ON.equals ON) {//Open screen Mscreenstatelistener.onscreenon ();} else if (Intent.ACTION_SCREEN_OFF.equals (ACTION)) {//Lock screen Mscreenstateli
Stener.onscreenoff (); else if (Intent.ACTION_USER_PRESENT.equals (ACTION)) {//unlock mscreenstatelistener.onuserpresent ();
/** * Starts listening on screen state * * @param listener */public void begin (Screenstatelistener listener) {Mscreenstatelistener =
Listener
Registerlistener ();
Getscreenstate (); /** * Get screen status */private void Getscreenstate () {PowerManager manager = (powermanager) mcontext. Getsystemservice (Con Text.
Power_service); if (Manager.isscreenon ()) {if (Mscreenstatelistener!= null) {Mscreenstatelistener.onscreenon ();}} else {if (mscreens
Tatelistener!= null) {Mscreenstatelistener.onscreenoff ();}} /** * Stop screen State listening/public void Unregisterlistener () {mcontext.unregisterreceiver (mscreenreceiver);}/** * Start screen State broadcast receiver/private void Registerlistener () {intentfilter filter = new Intentfilter (); Filter.addaction (Intent.action_scre
EN_ON);
Filter.addaction (Intent.action_screen_off);
Filter.addaction (intent.action_user_present);
Mcontext.registerreceiver (mscreenreceiver, filter); Public interface Screenstatelistener {//returned to caller screen state information public void Onscreenon (); public void Onscreenoff ();
public void onuserpresent (); }
}
2. Use
Package com.app.lib;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.widget.Toast;
public class Mainactivity extends Appcompatactivity {
private screenlistener screenlistener;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Screenlistener = new Screenlistener (mainactivity.this);
Screenlistener.begin (New Screenlistener.screenstatelistener () {
@Override public
void Onscreenon () {
Toast.maketext (mainactivity.this, "screen open", Toast.length_short). Show ();
@Override public
void Onscreenoff () {
toast.maketext (mainactivity.this, screen closed, toast.length_short). Show ();
}
@Override public
void Onuserpresent () {
toast.maketext (mainactivity.this, unlocked, toast.length_short). Show ();
}
});
}
}
The above is a small series to introduce the Android Monitor lock screen, unlock, open screen function code related knowledge, I hope to help you!