Hide UI when volume up down pressed in a dialog

耗尽温柔 提交于 2021-02-07 18:30:17

问题


Is there a way to hide the volume ui when the volume_up/volume_down key is pressed. I understand that it can be done with an activity when it doesnt seem to work when tiggered when a dialog is shown.

Is there an solution?


回答1:


This should work with the Dialog class:

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);
        }
    }
});

Like at Android: Hide Volume change bar from device?




回答2:


Try this code, it should disable the toast message of the Volume

@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;
}


来源:https://stackoverflow.com/questions/18335026/hide-ui-when-volume-up-down-pressed-in-a-dialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!