使用以下代码捕获音量控制按钮事件。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int action = event.getAction();
Log.i("Volume", "Volume " + keyCode + " " + action);
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
audio.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
audio.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
}
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
但是当显示音量条吐司时,它不再捕获音量按钮按下事件,除非吐司消失。
有什么解决办法吗?