Android: Hide Volume change bar from device?

前端 未结 1 1550
别那么骄傲
别那么骄傲 2020-12-16 22:13

is there a way to hide the volume change bar/notification (however you might call it..btw. how do you call it?) ?

相关标签:
1条回答
  • 2020-12-16 22:39

    It is the default post-honeycomb volume slider which appears when you press volume up/down buttons. You cannot hide it in every screen of the system, but you can hide it in your activities. Add this to an activity where you want to hide it and change the volume manually. Or you can just do nothing in case KeyEvent.KEYCODE_VOLUME_UP / DOWN if you don't want to change the volume. Remember to return true which menans you consumed the event. If you return false out there you will get double effect when the default volume behaviour will be triggered after your code.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_RAISE,
                        AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                manager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                        AudioManager.ADJUST_LOWER,
                        AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                return true;
    
            default:
                return super.onKeyDown(keyCode, event);
        }
    }
    

    Where AudioManager manager is retrieved as

    manager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
    
    0 讨论(0)
提交回复
热议问题