Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Detect Do Not Disturb status?

I was just wondering if there's a way to check if an Android device (21+) is in Do Not Disturb mode? I know there's AudioManager.RINGER_MODE_SILENT, but I was wondering if that pertains to this situation, or if there's a better way to check?

like image 214
Paul Ruiz Avatar asked Jul 13 '15 15:07

Paul Ruiz


People also ask

How do you tell if someone has you on Do Not Disturb?

Most obviously, you'll see a large dark grey notification on the lock screen. This will also tell you how long the mode will be on for. If there's room for it (the X- and 11-series handsets don't, because of the notch), a faint little crescent-moon icon will appear in the top bar on your iPhone or iPad's screen.

Does Do Not Disturb notify others?

Tap the Do Not Disturb icon. Do Not Disturb is now turned on — and no notifications will bother you — until you return to the Control Center and tap the icon to turn it off.


2 Answers

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.

like image 167
ssice Avatar answered Oct 17 '22 10:10

ssice


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.

like image 21
MattMatt Avatar answered Oct 17 '22 10:10

MattMatt