Android: Listen for power key press

Although I couldn’t capture the hardware key I ended up being able guess that the power key was pressed by using a broadcast receiver that listens for whether the screen is turned off or on. I used:

            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                    //do something 
            }

            else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                    //do something else
            }

I was able to be more sure that the power button was being pressed by having a count that would timeout after a couple second. This way I have the user press the power button three times to ensure that proper behavior.

Leave a Comment