2012-02-28 57 views
0

我有以下代碼:Android的音量鍵問題

@Override 
public boolean dispatchKeyEvent(KeyEvent event) { 
    int action = event.getAction(); 
    int keyCode = event.getKeyCode(); 
     switch (keyCode) { 
     case KeyEvent.KEYCODE_VOLUME_UP: 
      if (action == KeyEvent.ACTION_UP) { 
       int currentVolume = 0; 
       currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); 
       if(currentVolume != 15){ 
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume+1, 0); 
       } 
       volumeSlider.setProgress(currentVolume+1); 

      } 
      return true; 
     case KeyEvent.KEYCODE_VOLUME_DOWN: 
      if (action == KeyEvent.ACTION_DOWN) { 
       int currentVolume = 0; 
       currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); 
       if(currentVolume != 0){ 
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume-1, 0); 
       } 
       volumeSlider.setProgress(currentVolume-1); 
      } 
      return true; 
     default: 
      return super.dispatchKeyEvent(event); 
     } 
    } 

我有以下問題,與此代碼我的應用程序可以讓我按住音量向下鍵和不斷反應,但是當我按住我的設備上的音量增加鍵我的代碼只被觸發一次。

這是爲什麼?難道我做錯了什麼?

問候

回答

1

變化

if (action == KeyEvent.ACTION_UP) 

if (action == KeyEvent.ACTION_DOWN) 

爲KEYCODE_VOLUME_UP