android 修改音量键按下出现的进度条

主要实现代码

if (KeyEvent.KEYCODE_VOLUME_DOWN == keyCode || KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
        AudioManager audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);
    }

setStreamVolume 方法详解

streamType 流类型

index 音量

flag 音量模式

/**
 * Sets the volume index for a particular stream.
 * 

This method has no effect if the device implements a fixed volume policy * as indicated by {@link #isVolumeFixed()}. * @param streamType The stream whose volume index should be set. * @param index The volume index to set. See * {@link #getStreamMaxVolume(int)} for the largest valid value. * @param flags One or more flags. * @see #getStreamMaxVolume(int) * @see #getStreamVolume(int) * @see #isVolumeFixed() */ public void setStreamVolume(int streamType, int index, int flags) { IAudioService service = getService(); try { service.setStreamVolume(streamType, index, flags, getContext().getOpPackageName()); } catch (RemoteException e) { Log.e(TAG, "Dead object in setStreamVolume", e); } }

FLAG_PLAY_SOUND 调整音量时播放声音
FLAG_SHOW_UI 调整时显示音量条,就是按音量键出现的界面
FLAG_REMOVE_SOUND_AND_VIBRATE 无振动无声音
FLAG_ALLOW_RINGER_MODES : Whether to include ringer modes as possible options when changing volume.
FLAG_VIBRATE : Whether to vibrate if going into the vibrate ringer mode

你可能感兴趣的:(点滴日常)