Android monitor lock screen, unlock, open screen function code _android

Source: Internet
Author: User

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!

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.