如何以编程方式检测Android手机是否处于静音模式

时间:2010-01-12 11:13:41

标签: android

如何识别手机是否处于静音模式?

我使用的是Android 1.5。我尝试使用“android.provider.Settings.ACTION_SOUND_SETTINGS”。它不起作用。

2 个答案:

答案 0 :(得分:139)

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

答案 1 :(得分:0)

以下代码检查电话是否未处于静音模式,然后发出以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()