Android listens for screen lock, unlock, and open operations, and android screen lock

Source: Internet
Author: User

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

  

 

 

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.