Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Volume Buttons used in my application

Tags:

android

I need to use volume buttons to control a variable parameter in my application. I use Activity.onKeyDown to get notified when the button is pressed but the media volume is also increased.
Android is doing something like below when I press the volume key:

  1. increase media / ringtone volume
  2. pass the event to my application

Is there a way to avoid increasing the system volume and use volume key only for my application?

like image 559
Iuliu Atudosiei Avatar asked May 20 '10 14:05

Iuliu Atudosiei


People also ask

How do I control the volume on a different app?

For example, by swiping down from the top of the display, you'll see the volume settings for the alarm, multimedia, notifications, ringer, system, calls, and Bluetooth. Just tap on the option whose volume you want to change and adjust the volume with the slider that will appear.


1 Answers

You need to capture all actions:

@Override public boolean dispatchKeyEvent(KeyEvent event) {     int action = event.getAction();     int keyCode = event.getKeyCode();         switch (keyCode) {         case KeyEvent.KEYCODE_VOLUME_UP:             if (action == KeyEvent.ACTION_DOWN) {                 //TODO             }             return true;         case KeyEvent.KEYCODE_VOLUME_DOWN:             if (action == KeyEvent.ACTION_DOWN) {                 //TODO             }             return true;         default:             return super.dispatchKeyEvent(event);         }     } 
like image 96
yanchenko Avatar answered Sep 19 '22 13:09

yanchenko