(1) Procedures
Android API's AudioManager provides a method to adjust the volume of mobile phones.
audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
You can also adjust the voice mode of your phone to vibrate or mute.
AudioMa. setRingerMode (AudioManager. RINGER_MODE_NORMAL );
AudioMa. setRingerMode (AudioManager. RINGER_MODE_SILENT );
AudioMa. setRingerMode (AudioManager. RINGER_MODE_VIBRATE );
(2) Layout File
(3) code:
package com.liuzuyi.soundmode;import android.app.Activity;import android.content.Context;import android.media.AudioManager;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ProgressBar;public class MainActivity extends Activity { private ImageView myimage; private ImageButton downbutton; private ImageButton upbutton; private ImageButton normalbutton; private ImageButton mutebutton; private ImageButton vibratebutton; private ProgressBar myprogress; private AudioManager audioMa; private int volume; protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);audioMa =(AudioManager)getSystemService(Context.AUDIO_SERVICE);myimage = (ImageView)findViewById(R.id.myImage);myprogress =(ProgressBar)findViewById(R.id.myProgress);downbutton =(ImageButton)findViewById(R.id.downButton);upbutton =(ImageButton)findViewById(R.id.upButton);normalbutton=(ImageButton)findViewById(R.id.normalButton);mutebutton=(ImageButton)findViewById(R.id.muteButton);vibratebutton=(ImageButton)findViewById(R.id.vibrateButton);volume =audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}downbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}}});upbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}}});normalbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL); volume = audioMa.getStreamVolume(AudioManager.STREAM_RING); myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}});mutebutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT); volume = audioMa.getStreamVolume(AudioManager.STREAM_RING); myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}});vibratebutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); volume = audioMa.getStreamVolume(AudioManager.STREAM_RING); myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}});}}