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?
4 Answers
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.

- 2,242
- 33
- 30
-
1On my device, 1 is returned when the setting is priority only. – David Riha Mar 15 '17 at 12:42
-
@DavidRiha Thanks for pointing that out! I hadn't tried in over a year, and can now confirm that it returns 1 on my devices. I've updated my answer and Gist. – MattMatt Mar 15 '17 at 15:50
-
On some Xiaomi devices with Android 10 this solution doesn't work as your code can return a value of more than 3 (ex. there are a lot of cases when device returns 4) – Sergei Mikhailovskii Jul 29 '21 at 13:45
-
1@SergeiMikhailovskii This answer is over half a decade old, when this feature was not documented. In 2021, you should be referring to the official AutomaticZenRule documentation to understand how to get information on various manufacturer or user-defined DND states - there are far more than 4. – MattMatt Sep 06 '21 at 20:28
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.
-
On which of those does the ringtone get silenced? To me the only one that seems it's silenced for sure is `INTERRUPTION_FILTER_ALL`. I don't get when on the rest it gets affected, if at all. – android developer Dec 27 '18 at 15:22
-
It will depend user configuration. On Sound > Do not disturb > Priority only allows > Calls can be selected to be from anyone, contacts, starred contacts or no priority calls. On _ALARMS and _ALL every sound (except the alarm clocks) will get silenced. Depending on what you are building, the user may not expect you to block notifications they signaled as priority. – ssice Jan 07 '19 at 11:02
-
About "..._ALARMS", this also affects ringtones to be silenced? Is there an API to get the list of contacts that are white-listed? Or, given a phone number, to know if it will play a ringtone? – android developer Jan 07 '19 at 18:28
-
I am copying from the link in the documentation: "int INTERRUPTION_FILTER_ALARMS: Interruption filter constant - Alarms only interruption filter - **all notifications except those of category Notification.CATEGORY_ALARM are suppressed.**". The other is a different question for SO ;) – ssice Jan 08 '19 at 09:17
-
I don't get it then. They talk about notifications. What about ringtone? Is it included or not? Will it play or not? Or it's not defined? – android developer Jan 08 '19 at 09:37
-
-
To answer my own question, yes you can detect when it changes via the NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED broadcast. See: https://stackoverflow.com/questions/37620889/what-to-listen-for-to-detect-do-not-disturb-mode-changes-in-android – Flyview Dec 23 '19 at 05:02
You need to first add to the manifest file, permission to change the audio settings. To be able to set ringer mode to silent, you must ask permission to access notification policy
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
and then to detect the "do not disturb" you can do as follows
NotificationManager notificationManager = (NotificationManager) getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new
Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
return;
}
ToggleDoNotDisturb(notificationManager);
private void ToggleDoNotDisturb(NotificationManager notificationManager) {
if (notificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
audioToggle.setText(R.string.fa_volume_mute);
} else {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
audioToggle.setText(R.string.fa_volume_up);
}
}
also you need to check permissions
NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}

- 1,714
- 1
- 13
- 23
NotificationManager.Policy a = null;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
a = mNotificationManager.getNotificationPolicy();
Log.d(TAG, "onClickDND: "+a.priorityCallSenders);
Log.d(TAG, "onClickDND: "+a.priorityCategories);
}
output
--------------------------------------
FROM ANYONE
call senders : 0
categories : 109
Message : 0
State : 1
FROM CONTACTS ONLY
call senders : 1
categories : 109
Message : 1
State : 1
FROM STARRED CONTACTS ONLY
call senders : 2
categories : 109
Message : 2
State : 1
NONE
call senders : 2
categories : 97
Message : 2
State : 1

- 166
- 3
- 8