Android override volume button affects back button?

Using the code below, I stopped using the volume buttons if I don’t transmit sound (otherwise it annoyingly changes the ringer volume), but the back button does not work.

Pressing "back" should get to my phone desktop (or exit my application, as expected), but it does nothing. If I open the menu, Back will close the menu as it should, but I cannot leave the application.

I copied the code to other actions in my application, if I open another action in my application, because the "Back" button does not work, I can not return to the main screen :)

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { //Suppress the use of the volume keys unless we are currently listening to the stream if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) { if(StreamService.INT_PLAY_STATE==0){ return true; }else{ return false; } } if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN) { if(StreamService.INT_PLAY_STATE==0){ return true; }else{ return false; } } return false; 

Why is this happening?

+6
android override back volume android-audiomanager
source share
4 answers

Not tested, but I think you need to include another where you call super.onKeyDown, i.e.:

 if(keyCode == KeyEvent.KEYCODE_VOLUME_UP) { code } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { more code } else { super.onKeyDown(keyCode, event); } 

Otherwise, you will capture all key codes and return false after checking the volume codes.

+5
source share

A simpler and more reliable way to manage volume keys will always support media volumes in order to insert this line into their onCreate() activity:

 setVolumeControlStream(AudioManager.STREAM_MUSIC); 
+5
source share

Dude, just change the audio content to this activity on the multimedia volume:

http://developer.android.com/reference/android/media/AudioManager.html

EDIT:

 private AudioManager audio; 

Inside onCreate:

 audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

Override onKeyDown:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); return true; default: return false; } } 
+3
source share
 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { //your code return true; } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { //your code return true; } else { super.onKeyDown(keyCode, event); } return true; } 
0
source share

All Articles