67

How to identify whether the phone is in Silent mode or not?

I am using Android 1.5. I tried by using "android.provider.Settings.ACTION_SOUND_SETTINGS". It is not working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Raghu
  • 2,279
  • 6
  • 23
  • 21

3 Answers3

165

Use the getRingerMode() method in AudioManager.

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

switch (am.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        Log.i("MyApp","Silent mode");
        break;
    case AudioManager.RINGER_MODE_VIBRATE:
        Log.i("MyApp","Vibrate mode");
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        Log.i("MyApp","Normal mode");
        break;
}
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • Hi Dave is there any way to toggle Silent as well as vibrating mode programatically. – Vinayak Bevinakatti Jan 12 '10 at 13:23
  • 4
    `setRingerMode()` in `AudioManager` allows you to change the ringer mode. – David Webb Jan 12 '10 at 13:34
  • 1
    Hi Dave I can able to handle the silent mode using am.setRingerMode(AudioManager.RINGER_MODE_SILENT); but handling Vibrating mode using AudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); is not working any clue – Vinayak Bevinakatti Jan 12 '10 at 14:26
  • What is it that you're trying to do exactly? I don't think `setVibrateSettings()` does what you think it does. – David Webb Jan 12 '10 at 14:32
1

Following code checks if phone is not in silent mode then plays a beep, written in kotlin:

    val manager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, 10, 0)
    val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val player: MediaPlayer = MediaPlayer.create(applicationContext, notification)
    if(manager.ringerMode != AudioManager.RINGER_MODE_SILENT)
        player.start()
Mohsen Emami
  • 2,709
  • 3
  • 33
  • 40
0

If you want to listen for ringer mode changes means any change in Silent or Vibrate or Normal you can set up a broadcast receiver in your activity like below.

   private val volumeKeyReceiver: VolumeKeyReceiver by lazy {
        VolumeKeyReceiver()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerBroadcastListener()
    }

    private fun registerBroadcastListener() {
        val intentFilter = IntentFilter()
        intentFilter.addAction("android.media.RINGER_MODE_CHANGED")
        registerReceiver(volumeKeyReceiver, intentFilter)
    }

and unregister when the activity is destroyed like below.

 override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(volumeKeyReceiver)
    }

and register the receiver in manifest

 <receiver android:name=".VolumeKeyReceiver"/>

here goes the broadcast receiver

class VolumeKeyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        //Implement Logic
        val audioManager =
            context?.getSystemService(Context.AUDIO_SERVICE) as android.media.AudioManager
        when (audioManager.ringerMode) {
            android.media.AudioManager.RINGER_MODE_SILENT -> Log.d(
                "VolumeKeyReceiver",
                "Silent mode"
            )
            android.media.AudioManager.RINGER_MODE_VIBRATE -> Log.d(
                "VolumeKeyReceiver",
                "Vibrate mode"
            )
            android.media.AudioManager.RINGER_MODE_NORMAL -> Log.d(
                "VolumeKeyReceiver",
                "Normal mode"
            )
        }
    }
}
vikas kumar
  • 10,447
  • 2
  • 46
  • 52