how can I detect whether the android phone in Silent mode programmatically

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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;
}

Solution 2 - Android

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()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaghuView Question on Stackoverflow
Solution 1 - AndroidDave WebbView Answer on Stackoverflow
Solution 2 - AndroidMohsen EmamiView Answer on Stackoverflow