Search code examples
androidlockscreen

how to handle lock screen button input for android activity?


Suppose I have something like an alarm activity which launches the alarm through the lock screen, accomplished via

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Now, from here I could press the back button to kill the activity, but I also want a press of the lock button to silence the alarm. How would I go about creating a handler for presses of the lock screen button?


Solution

  • Listen to this broadcast intent

    ACTION_SCREEN_OFF
    

    You can switch off your alarm when you get this intent