I finally solved both issues. First of all, MEDIA_BUTTON
seems not to respond to volume changes. It's obviously responds only to PLAY
, PAUSE
, STOP
, etc. which is not what I want.
The simplest solution is still to handle the key events in OnKeyDown()
of every Activity
:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP ||
keyCode == KeyEvent.KEYCODE_MENU) {
showSoundSettingsDialog();
}
return true;
}
To write the code once, I moved this part into a BaseActivity from which the Activities inherit.
The second issue was solved by adding a simple OnKeyListener to the Dialog, as they have their own KeyEvents, what I didn't know so far.
builder.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
volumeUp();
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
volumeDown();
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
}
return true;
}
});
And that's it.