22

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sebastian Nowak
  • 5,607
  • 8
  • 67
  • 107

3 Answers3

41

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);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

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.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Hey Pratik, which method gets called when a phone call is received. Where should I put your code? Thanks! – Ruchir Baronia Nov 10 '15 at 05:34
  • @Rich You need to put in you application wherever you want just like device default Sound option in Setting screen and you can create same like this way or by simple way just provide option with switchview or checkbox option through manage it – Pratik Nov 10 '15 at 07:46
  • What about muting/unmuting the current call, meaning by using this : https://developer.android.com/reference/android/telecom/InCallService , or this: https://developer.android.com/reference/android/telecom/Call.html . – android developer May 07 '19 at 14:53
18
public void changeRingerMode(Context context){

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

    /**
    * To Enable silent mode.....
    */
    audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

    /**
    * To Enable Ringer mode.....
    */
    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

}
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0

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);
   if(off)
    {   //turn off ringing/sound
     //get the current ringer mode
     ringstate = audio.getRingerMode();
     if(ringstate!=AudioManager.RINGER_MODE_SILENT)
      audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
    }
  else
  {
    //restore previous state
    audio.setRingerMode(ringstate);


  }

}

This should do.

Makach
  • 7,435
  • 6
  • 28
  • 37
CanCoder
  • 1,073
  • 14
  • 20