Android: Detect Do Not Disturb status?
I'm bringing the necro with this post, but was just searching for a solution to the same problem and came past this post as I did, so am leaving a solution for future ref.
I couldn't find an "official" way to do this, but did find a value that can return the DnD state as an integer. The feature is called "zen_mode" internally, so you can check the current value with the following code:
Global.getInt(getContentResolver(), "zen_mode")
That'll return:
- 0 - If DnD is off.
- 1 - If DnD is on - Priority Only
- 2 - If DnD is on - Total Silence
- 3 - If DnD is on - Alarms Only
When the setting is priority only, it returns nothing, but you can figure out that state by assuming no news = Priority Messages mode. This must have been a bug, as now it's a year on, this now returns a value just like the other states. No idea when it was fixed, but it now works as you'd expect.
I tried with a settings observer too, which works but only for certain state transitions, so I think polling periodically is the best bet, until an "official" way to listen for this state change becomes available.
I've included both observer and polling methods of getting the value in this Gist. Hopefully it'll help/save someone else some time.
Update 15th March 2017: Thanks to David Riha for the comment. Added 1 - Priority Only
state to answer, revised the Gist to recognise that state, and to output unknown values to log in case any other devices or future updates decide to return a different value.
With pointers from Android how to turn on do not disturb (dnd) programmatically:
In SDK 23, android.app.NotificationManager provides the interface you need, namely NotificationManager.getCurrentInterruptionFilter()
.
It should return one of:
INTERRUPTION_FILTER_PRIORITY
INTERRUPTION_FILTER_ALARMS
INTERRUPTION_FILTER_NONE
INTERRUPTION_FILTER_ALL
INTERRUPTION_FILTER_UNKNOWN
According to Google Help on Nexus Devices Do not Disturb is a feature of Android >= 6.0, so SDK 23 should be reasonable to ask. If it is not, I think it would be reasonable to ask why, to be able to provide a workaround.