Hardware buttons

Is there any way to listen for hardware button events (volume up/down, power button) in the background?


I am developing a safety app and would like to trigger an emergency event on e.g. pressing the volume button OR power button 5 times continuously.


The app currently has the feature, but the user has to open the app to trigger it and that's the initial criticism. the user won't have enough time to open the app in an emergency.

Replies

No, you can't with published API.


And it is on purpose, to avoid changing the expected behavior of buttons .

If your app is in foreground, you can keep the buttons you need on screen for easy access to user.

if app is in background, changing the behavior of buttons would be a problem for user (launching an alert as he/she is just trying to change volume for music)

if app is closed, nothing can happen of course.


So, you should look for a different design.

is there any other design choice to handle this scenario ? other than opening the app and pressing on screen button.

Anything you can only be done from app.


I do not see other scenario (if you don't want your app being rejected)

See guidelines:

Apps that alter the functions of standard switches, such as the Volume Up/Down and Ring/Silent switches, will be rejected