Android screen and unlock event broadcast listening

Source: Internet
Author: User


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 );}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.