Today, let’s briefly talk about how to monitor whether the mobile phone screen is locked.
Implementation method:
1) Receive broadcasts through BroadcastReceiver Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF can determine whether the screen state is locked, but the broadcast will only be sent when the screen state changes;
2) If you want to get the screen state before the screen state changes, you can call the isScreenOn method of PowerManager through the reflection mechanism.
For specific implementation, see the code:
Directly upload the code:
1. Define a class that receives broadcasts
package ; import ; import ; import ; import ; import ; /** * Created by ${zyj} on 2016/6/21. */ public class ScreenListener { private Context mContext; private ScreenBroadcastReceiver mScreenReceiver; private ScreenStateListener mScreenStateListener; public ScreenListener(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 = (); if (Intent.ACTION_SCREEN_ON.equals(action)) { // Open the screen (); } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // Lock screen (); } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // Unlock (); } } } /** * Start listening to screen status * * @param listener */ public void begin(ScreenStateListener listener) { mScreenStateListener = listener; registerListener(); getScreenState(); } /** * Get screen status */ private void getScreenState() { PowerManager manager = (PowerManager) mContext .getSystemService(Context.POWER_SERVICE); if (()) { if (mScreenStateListener != null) { (); } } else { if (mScreenStateListener != null) { (); } } } /** * Stop screen status monitoring */ public void unregisterListener() { (mScreenReceiver); } /** * Start screen status broadcast receiver */ private void registerListener() { IntentFilter filter = new IntentFilter(); (Intent.ACTION_SCREEN_ON); (Intent.ACTION_SCREEN_OFF); (Intent.ACTION_USER_PRESENT); (mScreenReceiver, filter); } public interface ScreenStateListener {// Return to the caller's screen status information public void onScreenOn(); public void onScreenOff(); public void onUserPresent(); } }
2. Use:
package ; import .; import ; import ; public class MainActivity extends AppCompatActivity { private ScreenListener screenListener ; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); screenListener = new ScreenListener( ) ; (new () { @Override public void onScreenOn() { ( , "Screen is on" , Toast.LENGTH_SHORT ).show(); } @Override public void onScreenOff() { ( , "Screen is off" , Toast.LENGTH_SHORT ).show(); } @Override public void onUserPresent() { ( , "Unlocked" , Toast.LENGTH_SHORT ).show(); } }); } }
Summarize
The above is the example code for whether the Android monitor screen is locked by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!