4

有没有办法在按下 volume_up/volume_down 键时隐藏音量 ui。我知道它可以通过一个活动来完成,当它在显示对话框时被触发时似乎不起作用。

有解决办法吗?

4

2 回答 2

7

这应该适用于 Dialog 类:

AudioManager man = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
dialog.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE,
                    AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                man.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER,
                    AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
           default:
                return super.onKeyDown(keyCode, event);
        }
    }
});

就像在Android 上一样:从设备中隐藏音量更改栏?

于 2013-08-20T13:01:52.540 回答
3

试试这个代码,它应该禁用 Volume 的 toast 消息

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        //Do something to hide the view
        AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
    }
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)){
        //Do something to hide the view
        AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
    }
    return true;
}
于 2013-08-20T12:46:02.160 回答