Android listens for screen lock, unlock, and open operations, and android screen lock
1. First define 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; private ScreenStateListener mScreenStateListener; public ScreenList Ener (Context context) {mContext = context; mScreenReceiver = new ScreenBroadcastReceiver ();}/*** screen status broadcast receiver */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) {// enable mScreenStateListener. onScreenOn ();} el Se if (Intent. ACTION_SCREEN_OFF.equals (action) {// lock screen mScreenStateListener. onScreenOff ();} else if (Intent. ACTION_USER_PRESENT.equals (action) {// unlock mScreenStateListener. onUserPresent () ;}}/ *** start to listen to screen status ** @ param listener */public void begin (ScreenStateListener listener) {mScreenStateListener = listener; registerListener (); getScreenState ();}/*** get screen status */private void getS CreenState () {PowerManager manager = (PowerManager) mContext. getSystemService (Context. POWER_SERVICE); if (manager. isScreenOn () {if (mScreenStateListener! = Null) {mScreenStateListener. onScreenOn () ;}} else {if (mScreenStateListener! = Null) {mScreenStateListener. onScreenOff () ;}}/ *** stop screen status listener */public void unregisterListener () {mContext. unregisterReceiver (mScreenReceiver);}/*** start the screen status broadcast receiver */private void registerListener () {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);} public interface ScreenStateListener {// return the screen status information to the caller 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, "the screen is on", Toast. LENGTH_SHORT ). show () ;}@ Override public void onScreenOff () {Toast. makeText (MainActivity. this, "the screen is closed", Toast. LENGTH_SHORT ). show () ;}@ Override public void onUserPresent () {Toast. makeText (MainActivity. this, "Unlocked", Toast. LENGTH_SHORT ). show ();}});}}