Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check volume button usage when screen is off

For this question I'm going to quote another user who got no response to their question:

I've written an Andoid app that uses the hardware Volume buttons for another purpose.

It works fine if the app is running and visible, but when I turn the screen off or let it time out, the button clicks don't get into my handlers.

Does anyone know if there is a way to detect these button clicks when the screen is off? Source: AV695's question

I'm working on an app myself that makes use of the volume buttons, but as this user also noted, the normal behavior of checking buttons with onKeyPress stops working once the screen is off. This is because the Activity gets paused on screen off.

Is there a way to keep the activity running while the screen is off, or check for the usage of the volume buttons when the screen is off? I tried using a Service for this before but it's impossible to check for the volume keys like that as noted by Commonsware.

like image 786
Sander van't Veer Avatar asked Apr 21 '12 19:04

Sander van't Veer


People also ask

How do I check my volume buttons?

Open Settings on your Android phone by clicking the gear icon. Then, head over to the sound and vibrations option. You will see a few volume sliders adjusting the volume of media, ringtone, notifications, and alarms, now you just need to adjust the volume and that's it.

How do I turn on my phone volume button?

Yes, you can. In Android phones, you can remap your volume button (either up or down) to open the power menu using apps like Button Mapper. Open the app, and give permissions in the accessibility settings. After giving the required permissions, you will be greeted with options to remap your buttons.


1 Answers

I doubt that this is supported (without resorting to a battery-draining wakelock) at either the platform, kernel, or underlying radio firmware levels without modifications to the last to bring volume presses during sleep to the attention of the kernel.

Within the realm of reasonable system-ROM modifications, a more reasonable one might be to modify an existing open source ROM for the device to insert some custom platform level code into the handling of the power button usually used to wake up the device preparatory to unlocking it - that at least we know does get the attention of the kernel. That code could then inform the user by sound or vibration if there are unacknowledged notifications.

You could optionally wait briefly, check device orientation, or look for another key press to avoid doing this in an annoying way when the user is holding the device outside their pocket and trying to unlock it.


Or you could not use the volume key and just set a timer to wake up every 15 minutes and vibrate if there are unacknowledged notifications, avoiding the need to fumble in ones pockets.


You mention it's a custom request: if implies it's one off or low-volume, another option to consider would be that a few vendors have "bluetooth watches" out with an SDK that lets you push notifications from an android device.

If you can capture the notification when it's generated, you could push it to the user's wrist, and then let the phone go back to sleep.

like image 175
Chris Stratton Avatar answered Oct 22 '22 08:10

Chris Stratton