Get System Volume
Get the ringtone and volume of your Android phone through the program. Also, the way to set the ringtone and volume is simple!
The way to set the volume is also simple, Audiomanager provides the method:
Publicvoidsetstreamvolume (intstreamtype,intindex,intflags) where Streamtype has built-in constants, go to the documentation to see them.
Java code:
Audiomanager Maudiomanager = (audiomanager) getsystemservice (Context.audio_service);
Call volume
int max = Maudiomanager. Getstreammaxvolume (Audiomanager.stream_voice_call);
int current = Maudiomanager. Getstreamvolume (Audiomanager.stream_voice_call);
LOG.D ("Vioce_call", "Max:" + Max + "Current:" + current);
System Volume
max = Maudiomanager. Getstreammaxvolume (Audiomanager.stream_system);
Current = Maudiomanager. Getstreamvolume (Audiomanager.stream_system);
LOG.D ("SYSTEM", "Max:" + Max + "Current:" + current);
Ringtone Volume
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_ring);
Current = Maudiomanager.getstreamvolume (audiomanager.stream_ring);
LOG.D ("RING", "Max:" + Max + "Current:" + current);
Music volume
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_music);
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_music);
LOG.D ("MUSIC", "Max:" + Max + "Current:" + current);
Prompt for sound volume
max = Maudiomanager.getstreammaxvolume (audiomanager.stream_alarm);
Current = Maudiomanager.getstreamvolume (Audiomanager.stream_alarm);
LOG.D ("ALARM", "Max:" + Max + "Current:" + current);
Ps:
Only the multimedia volume is allowed to be adjusted during the game, and the call volume is not allowed.
Setvolumecontrolstream (Audiomanager.stream_music);
Do not move for a long time, do not allow black screen, View.setkeepscreenon (true);
It is estimated that registration permission is required in the manifest file.
Adjust Media volume
AudioManager audio = (AudioManager) getSystemService(Service.AUDIO_SERVICE);
OnKeyDown method for overriding Activity
?@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: audio.adjustStreamVolume( AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI); return true; case KeyEvent.KEYCODE_VOLUME_DOWN: audio.adjustStreamVolume( AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI); return true; default: break; } return super.onKeyDown(keyCode, event);}
[Turn]android system to obtain and set various volume