Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Phone playing notification sounds even when on silent? Should I be checking for silent mode?

Tags:

android

I was very surprised to find in the app I am currently programming that when I make it play a notification sound, it plays regardless of whether the phone has been set to silent or not!

Surely the phones silent mode should be an overriding feature or am I meant to be checking? I had a quick look at the documentation but did not see anything saying this? Am I missing something?

This is my notification code:

    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(300);

        Uri alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        MediaPlayer mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setDataSource(this, alert);

        final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.setLooping(false);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }

Thanks for any feedback here!

Bex

like image 344
Bex Avatar asked May 25 '11 10:05

Bex


2 Answers

Not sure about the documentation you checked, but on Nexus One below Silent mode checkbox it clearly says:

Silence all sounds except media & alarms.

In your example you are playing alarm (not notification), so it is correct.

like image 163
Zelimir Avatar answered Oct 19 '22 11:10

Zelimir


Based on the testing results it seems that you're the one that needs to check it:

if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
    // Play the sound
}
like image 4
Kamen Avatar answered Oct 19 '22 11:10

Kamen