1

我希望用户能够长按 volumeUp 硬件按钮来跳过歌曲,并在短按时执行常规的 volumeUp 操作。

我能够区分两者(我找到了这个解决方案,使用 onKeyDown、onKeyLongPress 和 onKeyUp 之间的标志)但我想知道当按下音量增大按钮时我是否仍然可以调用标准/超级操作。我似乎无法弄清楚何时调用 volumeUp 操作(在 onKeyDown 或 onKeyUp 事件中)以及在哪里调用它。

还是我应该编写自己的函数来更改音量?

谢谢。

我的代码:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        event.startTracking();
        if (bLong) {
            bShort = false;
            return true;
        } else {

            bShort = true;
            bLong = false;

            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        if (bShort) {
            bShort = false;
            bLong = false;
            if (mp != null) {
                //HERE IS WHERE I WANT TO CALL THE VOLUME-UP ACTION
            }
            return true;
        }
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        if (bRunning && mp != null) {
            playNextSong();
        }
        bShort = false;
        bLong = false;
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
4

1 回答 1

3

看看也许这会对你有所帮助。

public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();

        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_DOWN && event.isLongPress()) {
            //(skipping a song)
            }
            if (action == KeyEvent.ACTION_UP) {           
            //(vol up)
            }
            return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_UP)

        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}
于 2013-02-19T13:52:43.637 回答