Android: Hide Volume change bar from device?

笑着哭i 提交于 2019-12-04 23:40:15

问题


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

i attached a screenshot above. this bar is shown everytime i change the volume (at least on my nexus test device). or is this a nexus special?


回答1:


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


来源:https://stackoverflow.com/questions/14563208/android-hide-volume-change-bar-from-device

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