To listen to the screen screen_on and screen_off actions in a program, you can monitor the screen lock status to implement your own functions. The strange thing is that these two actions can only be listened to after they are registered in the form of code, and they cannot be registered in androidmanifest. xml. I checked it online. It turns out that powermanager imposes a limit when sending this broadcast. The limit is that only the Register can receive the broadcast from the receiver in the code. We hereby record it!
Public class screenactionreceiver extends broadcastreceiver {private string tag = "screenactionreceiver"; private Boolean isregisterreceiver = false; @ overridepublic void onreceive (context, intent) {string action = intent. getaction (); If (action. equals (intent. action_screen_on) {logcat. D (TAG, "unlock broadcast screen... ");} else if (action. equals (intent. action_screen_off) {logcat. D (TAG, "screen lock broadcast... ");}} Public void registerscreenactionreceiver (context mcontext) {If (! Isregisterreceiver) {isregisterreceiver = true; intentfilter filter = new intentfilter (); filter. addaction (intent. action_screen_off); filter. addaction (intent. action_screen_on); logcat. D (TAG, "register screen unlock, lock broadcast receiver... "); mcontext. registerreceiver (screenactionreceiver. this, filter) ;}} public void unregisterscreenactionreceiver (context mcontext) {If (isregisterreceiver) {isregisterreceiver = false; logcat. D (TAG, "unlock the screen and lock the broadcast receiver... "); mcontext. unregisterreceiver (screenactionreceiver. this );}}}