0

I'm writing app that should play sound notification even in silent mode. I don't want to temporarily turn off Silent and roll it back late. I'm using ALARM stream for that now. Usually it works proper, because by default ALARM stream not muted in silent. But sometimes it is.

How can I understand if stream currently muted (in silent mode)? How can I unmute it (without switching to normal mode)?

nickkadrov
  • 488
  • 2
  • 12

1 Answers1

1

Answering my own question:

The solution was found in Android OS sources (in standard Alarm app).

I had found way to check and set option "Alarm in Silent":

private static final int ALARM_STREAM_TYPE_BIT =
        1 << AudioManager.STREAM_ALARM;

public static void setAlarmInSilent(boolean on) {
    int ringerModeStreamTypes = Settings.System.getInt(
            context.getContentResolver(),
            Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);

    if (on) {
        ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT;
    } else {
        ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT;
    }

    Settings.System.putInt(context.getContentResolver(),
            Settings.System.MODE_RINGER_STREAMS_AFFECTED,
            ringerModeStreamTypes );
}

Link to the Android src: https://android.googlesource.com/platform/packages/apps/AlarmClock/+/donut-release2/src/com/android/alarmclock/SettingsActivity.java

nickkadrov
  • 488
  • 2
  • 12
  • Thanks I have the same problem. Is this system wide setting? So I have to set it back after playing the sound? How did you solved that? – chrisonline Jan 01 '14 at 14:26
  • Yes it system wide settings. So, I'm restoring it after. – nickkadrov Jan 02 '14 at 15:49
  • But when? After you call notificationManager.notify()? – chrisonline Jan 02 '14 at 19:04
  • The right answer "do it when it's not needed anymore". In my cases I'm rolling back after nights profile ends (I developed DND app). And keep in mind that you should restoring to the previous value, not permanently turning it of. Otherwise user can miss morning alarm in silent mode when he expect it. – nickkadrov Jan 03 '14 at 20:46