Bjarte Aune Olsen Bjarte Aune Olsen - 1 year ago 1181
Android Question

In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent)

I have an app that mutes the phone by using AudioManager and setting ringer mode to silent with this code:

AudioManager audioManager =
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
try {
} catch (Exception e) {

This works with Android 6, but now with Android 7, I get the following error:

System.err: java.lang.SecurityException: Not allowed to change Do Not Disturb state
System.err: at android.os.Parcel.readException(
System.err: at android.os.Parcel.readException(
System.err: at$Stub$Proxy.setRingerModeExternal(
System.err: at
System.err: at controllers.SearchResultController.mutePhone(

Are there any new permissions I need to ask for to make this work?

I looked through the Android permissions list, but couldn't find any that seemed relevant.

Answer Source

Thanks for your answers, here is a little more detail.

To be able to set ringer mode to silent, you must have permission to access notification policy (like @ucsunil said).

First check if you have this permission. If you do not, open the settings for Do Not Disturb access for your app:

NotificationManager notificationManager = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    && !notificationManager.isNotificationPolicyAccessGranted()) {

    Intent intent = new Intent(


When you run startActivity(), Android opens the Do Not Disturb access settings for your app.

What confused me, was that the way to ask for this permission is completely different from other permissions.

Just for reference, here is the way to ask for permission READ_CONTACTS:

    && ActivityCompat.checkSelfPermission(activity,
        == PackageManager.PERMISSION_DENIED) {

        new String[]{ Manifest.permission.READ_CONTACTS },
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download