Android detect phone lock event-ThrowExceptions

Exception or error:

I want to be able to detect the phone lock event. When my app is running, if I press the red button (call end button/power button), the phone gets locked and the screen goes blank. I want to be able to detect this event, is it possible?

How to solve:

Have a Broadcast Receiver




Related: Read CommonsWare’s Answer Here.


Alternatively you could do this:

protected void onPause()

    // If the screen is off then the device has been locked
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    boolean isScreenOn;
        isScreenOn = powerManager.isInteractive();
    } else {
        isScreenOn = powerManager.isScreenOn();

    if (!isScreenOn) {

        // The screen has been locked 
        // do stuff...

Leave a Reply

Your email address will not be published. Required fields are marked *