android音量键问题

时间:2012-02-28 15:05:55

标签: 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 个答案:

答案 0 :(得分:1)

更改

if (action == KeyEvent.ACTION_UP)

on

if (action == KeyEvent.ACTION_DOWN)

表示KEYCODE_VOLUME_UP