Android mute/unmute phone-ThrowExceptions

Exception or error:

My goal is to support 2 operations:

  • mute phone (possibly with vibrations enabled/disabled), so when a call or sms is received it won’t make noise

  • unmute phone and restore the volume to the state before muting phone

How can I do this? What permissions are required in AndroidManifest?

How to solve:

This is the permission for vibrate into the manifest file

<uses-permission android:name="android.permission.VIBRATE" />

this is for to put the device in silent mode with vibrate

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

this is for to put into the ringing mode

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);


public void changeRingerMode(Context context){

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    * To Enable silent mode.....

    * To Enable Ringer mode.....



If what you want is to disable sound and restore the sound setting to previous state, this worked for me.

static int ringstate = 0;
private void soundOn(boolean off){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    {   //turn off ringing/sound
     //get the current ringer mode
     ringstate = audio.getRingerMode();
      audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
    //restore previous state



This should do.

Leave a Reply

Your email address will not be published. Required fields are marked *