Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
} catch (Exception e) {
    e.printStackTrace();
}

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(Parcel.java:1683)
System.err: at android.os.Parcel.readException(Parcel.java:1636)
System.err: at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:962)
System.err: at android.media.AudioManager.setRingerMode(AudioManager.java:1022)
System.err: at controllers.SearchResultController.mutePhone(SearchResultController.java:185)

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.

like image 430
Bjarte Aune Olsen Avatar asked Aug 25 '16 17:08

Bjarte Aune Olsen


3 Answers

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

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

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

Then, 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);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    && !notificationManager.isNotificationPolicyAccessGranted()) {

    Intent intent = new Intent(
                        android.provider.Settings
                        .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

    startActivity(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:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    && ActivityCompat.checkSelfPermission(activity,
        Manifest.permission.READ_CONTACTS)
        == PackageManager.PERMISSION_DENIED) {

    ActivityCompat.requestPermissions(activity,
        new String[]{ Manifest.permission.READ_CONTACTS },
            REQUEST_CODE_READ_CONTACTS);
}
like image 74
Bjarte Aune Olsen Avatar answered Nov 01 '22 06:11

Bjarte Aune Olsen


The access you are looking for is:

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

You need to request the user to provide you with this specific permission.

like image 33
ucsunil Avatar answered Nov 01 '22 07:11

ucsunil


ACCESS_NOTIFICATION_POLICY is considered a "normal" permission which means you do NOT have to request permission at runtime. You only have to add the permission to AndroidManifest.xml

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

Then, to mute all notifications you can set the Interruption filter to INTERRUPTION_FILTER_NONE

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();
            notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
        }

INTERRUPTION_FILTER_ALARMS - Alarms only
INTERRUPTION_FILTER_ALL - Normal filter
INTERRUPTION_FILTER_NONE - No interruptions (MUTE)
INTERRUPTION_FILTER_PRIORITY - Priority interruptions

I use

 int previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();

to reset the filter later when onPause() or onStop()

like image 37
Winston Brown Avatar answered Nov 01 '22 08:11

Winston Brown