54

How do I turn on/off 'do not disturb' (dnd) programmatically in Android? Is was hoping this would give me something but it doesn't:

Settings.System.putInt(getContentResolver(), Settings.System.DO_NOT_DISTURB, 1);
Andrew
  • 3,545
  • 4
  • 31
  • 37

3 Answers3

50

I found this solution:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);

It requires:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

Which as far as I can tell does not pop up a request dialog when doing requestPermissions(). It has to be granted through the settings menu Settings -> Sound & Notifcation -> Do Not Disturb Access.

This is on SDK 23 Marshmallow.

Jordan Parsons
  • 664
  • 7
  • 6
  • 7
    `setInterruptionFilter` is for Lollipop notification priority filter. Use `setNotificationPolicy` instead. – Jason Hu Feb 16 '16 at 22:25
  • Will it also hide heads-up notifications? Or just the sound&vibration? How would you block them on Lollipop and below? – android developer Oct 12 '16 at 23:44
  • 2
    @JasonHu both methods require Android API 23 and above, which is Android M . – android developer Oct 13 '16 at 00:11
  • This requires me to pop up a notification? – IgorGanapolsky May 14 '20 at 18:55
  • @androiddeveloper it seems like it sadly does not hide heads-up notifications –  Jul 13 '20 at 10:41
  • with setNotificationPolicy you can set the Do Not Disturb "Allow Interruptions" policies like NotificationManager.Policy.PRIORITY_CATEGORY_ALARM ( allow alarm ) with setInterruptionFilter you set the actual Don Not Disturb on or off where : NotificationManager.INTERRUPTION_FILTER_PRIORITY = set DND on with the setNotificationPolicy settings NotificationManager.INTERRUPTION_FILTER_NONE = set DND on for every interuption NotificationManager.INTERRUPTION_FILTER_ALL = set DND off – shazbot Oct 14 '21 at 09:10
37

Just improving the answer from Jordan Parsons. To set the priority you need a permission that can only be obtained by asking the user opening an Activity. An activity will be prompted asking if you want to enable the permission for Do Not Disturb to you application.

This Activity can be opened this way:

 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);
 }

After granting this permission, this setting can be changed.

Vitor Braga
  • 2,173
  • 1
  • 23
  • 19
7

For those struggling with LG G4, there is a workaround.

LG didn't setup the settings option to grant

permission.ACCESS_NOTIFICATION_POLICY 

Therefore it crashes when it receives

ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS 

However, LG did setup the option to grant

permission.BIND_NOTIFICATION_LISTENER_SERVICE

And once this permission is granted, so is

permission.ACCESS_NOTIFICATION_POLICY 

(at least on LG G4). This option is under Settings->General->Security->Notification access. For the user to be able to grant this permission, your app needs to request for it. This is done by declaring your service, assuming you have one for your app, as follows in the manifest:

<service android:name="myService"
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>
Adam
  • 143
  • 3
  • 18
Yoav Sharon
  • 71
  • 1
  • 2
  • Hi, I need to request this permission from an Activity. How can I do that? – Slava Kamzen Jul 28 '17 at 08:12
  • 1
    Then should we start the activity with intent ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS from myService ? Can you please explain little more. – mifthi Sep 21 '17 at 15:28