Using APIs to retrieve and set ringtones, volume, mute, and speakers is actually quite simple.
[Java] view plain
Copy
// Obtain an audiomanager
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 );
// Sound volume prompt
Max = maudiomanager. getstreammaxvolume (audiomanager. stream_alarm );
Current = maudiomanager. getstreamvolume (audiomanager. stream_alarm );
Log. D ("alarm", "MAX:" + MAX + "current:" + current );
The method for setting the volume is also very simple. audiomanager provides the method:
[Java] view plain
Public void setstreamvolume (INT streamtype, int index, int flags)
Streamtype is the ringtone type, such as audiomanager. stream_voice_call and audiomanager. stream_system. The index is the volume size.
Falgs is the flag.
Set Vibration:
</PRE> <PRE name = "code" class = "Java"> <span style = "font-family: Arial, Helvetica, sans-serif; "> <span style =" white-space: normal; "> <span style =" font-family: monospace; "> <span style =" white-space: Pre; "> </span> <PRE name =" code "class =" Java "> mvibrator
= (Vibrator) mcontext. getsystemservice (service. vibrator_service );
Long [] pattern = {150,100}; // off/On/Off/on...
Mvibrator. vibrate (pattern,-1 );
Mute:
Set the system sound to 0.
// Set mute during a call
System. Out. println ("ismicrophonemute =" + audiomanager. ismicrophonemute ());
Audiomanager. setmicrophonemute (! Audiomanager. ismicrophonemute ());
// Set hands-free during calls
System. Out. println ("isspeakerphoneon =" + audiomanager. isspeakerphoneon ());
Audiomanager. setspeakerphoneon (! Audiomanager. isspeakerphoneon ());
// Do not forget to add permissions
<Uses-Permission Android: Name = "android. Permission. modify_audio_settings"/>
From: http://blog.csdn.net/qianhen136/article/details/7409775