Disable the Volume Toast

北慕城南 提交于 2019-12-22 05:25:44

问题


I'm trying to increase the volume using AudioManager.

But it showing Native Android Volume UI(Volume Seekbar) toast.I want to disable this. I know this can be possible in Activity using Key Event but i want to do it through service. How to disable the toast? Here is a screenshot of the toast:


回答1:


The AudioManager-class offers the following methods to adjust the volume of certain streams:

  • adjustVolume(int, int)
  • adjustStreamVolume(int, int, int)
  • adjustSuggestedStreamVolume(int, int, int)
  • setStreamVolume(int, int, int)

All those methods take a flag-parameter as their last argument. The flag which is interesting for you is the FLAG_SHOW_UI which:

Show a toast containing the current volume.

So, to get rid of the Toast, don't supply this flag (or the int-value 1) but supply all your other flags (if needed) or just 0:

AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);

The above code-snippet works for me on Android 4.0.4 (Motorola Xoom) and Android 2.3.5 (HTC Desire HD).




回答2:


I know it's a little bit late, but I think I've managed to hide the volume toast using this:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            //volumeDown();
            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
            //volumeUp();
            return true;
    }
    return super.onKeyUp(keyCode, event);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            volumeDown();
            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
            volumeUp();
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

private void volumeUp(){
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, SOME_RANDOM_VALUE, AudioManager.FLAG_VIBRATE);
}

Ovveriding only one method from onKeyUp and onKeyDown didn't prevent displaying the toast (even when using flags).

It's a little bit weird - like Android tries to display the toast twice.



来源:https://stackoverflow.com/questions/12163933/disable-the-volume-toast

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