我想根据物理音量键更新我的搜索栏,但我的代码不起作用! 请帮帮我..
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
customSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
return true;
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:0)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
customSeekBar.setProgress(volume_level-1);
am.adjustStreamVolume( AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,0);
}else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
//Do something
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
customSeekBar.setProgress(volume_level+1);
am.adjustStreamVolume( AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,0);
}
return true;
}
请尝试让我知道..
答案 1 :(得分:0)
KeyEvent.KEYCODE_VOLUME_DOWN -> {
audioManager?.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI)
return true
}
KeyEvent.KEYCODE_VOLUME_UP -> {
audioManager?.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI)
return true}