这个网上貌似很多回答,大多数是转载,监听onkeyDown消息没错,可是好多都没有给出获取当前音量的方法,基本上没法直接用。下面这个测试是有效的。
[java] view plaincopy
1.AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
2.@Override
3.public boolean onKeyDown(int keyCode, KeyEvent event) {
4. switch (keyCode) {
5. case KeyEvent.KEYCODE_VOLUME_UP:
6. audio.adjustStreamVolume(
7. AudioManager.STREAM_MUSIC,
8. AudioManager.ADJUST_RAISE, // 增加音量
9. AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
10. return true;
11. case KeyEvent.KEYCODE_VOLUME_DOWN:
12. audio.adjustStreamVolume(
13. AudioManager.STREAM_MUSIC,
14. AudioManager.ADJUST_LOWER,//减少音量
15. AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
16. return true;
17. default:
18. break;
19. }
20. return super.onKeyDown(keyCode, event);
21.}
转载于:https://www.cnblogs.com/ywq-come/p/5925621.html