Android Monitor lock screen/Open screen event

Source: Internet
Author: User

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

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.