Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the behavior of the volume buttons in an Android application?

Tags:

I'd like to use the volume buttons for something else in my Android application. The Dolphin browser does this I am told. Anyone know how?

like image 869
Jim Blackler Avatar asked Mar 02 '10 22:03

Jim Blackler


People also ask

How do I change the volume control buttons?

Use Virtual volume slider 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 you lock volume buttons on Android?

Set the volume of your phone to where you would like it to stay. Once that is set, open the app and tap the box that says “Lock Volume at Current Level.” There you will also see the “Lock Voice Call Volume” and “Lock Media Volume.”


2 Answers

I imagine it looks something like this:

public boolean onKeyDown(int keyCode, KeyEvent event)  {     if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {         // Do your thing         return true;    } else {        return super.onKeyDown(keyCode, event);     } } 
like image 65
Segfault Avatar answered Jan 03 '23 11:01

Segfault


in your activity,

 @Override     public boolean dispatchKeyEvent(KeyEvent event) {         int keyCode = event.getKeyCode();         switch (keyCode) {         case KeyEvent.KEYCODE_VOLUME_UP:         case KeyEvent.KEYCODE_VOLUME_DOWN:             return true;         default:             return super.dispatchKeyEvent(event);         }     } 
like image 23
Mohammad Ersan Avatar answered Jan 03 '23 12:01

Mohammad Ersan