6

How can I silence the android phone in java? A code sample is VERY helpful.

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

2 Answers2

20

You can use the AudioManager class.

In this class you're looking for setRingerMode() function.

AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

The values you can pass into the function are:

The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.

You have to add this into the manifest file:

android.permission.MODIFY_AUDIO_SETTINGS
Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • Is there something aditional to this that must be done? I have pretty much this exact same code (including the permission) but I always get an exception when trying to get the SystemService. – PedroC88 Dec 11 '10 at 17:51
3

Use the AudioManager class's methods to silence audio.

For instance, to turn off the ringer use manager.setRingerMode(AudioManager.RINGER_MODE_SILENT)

You can get an instance of the AudioManager by calling Context.getSystemService(Context.AUDIO_SERVICE)


http://developer.android.com/reference/android/media/AudioManager.html#setRingerMode(int)

McStretch
  • 20,495
  • 2
  • 35
  • 40