android监听音量上下键调节音量

AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);  
@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_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);  
    return true;  
   case KeyEvent.KEYCODE_VOLUME_DOWN:  
    audio.adjustStreamVolume(  
        AudioManager.STREAM_MUSIC,  
        AudioManager.ADJUST_LOWER,//减少音量  
        AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);  
    return true;  
    default:  
    break;  
   }  
   return super.onKeyDown(keyCode, event);  

你可能感兴趣的:(android)