My app sets the ringer mode to silent but with Android N I get a java.lang.SecurityException: Not allowed to change Do Not Disturb state
.
I followed the steps from this post and the following code will open an activity which contains a toggle button for enabling the permission to change the ringer mode. This activity can be "backed" from without the enabling of the permission.
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
The problem is that I want to ensure that the permission is granted before attempting to use the silent ringer mode functionality.
I'm adding a CheckBoxPreference to enable and disable of this ringer mode toggling. So if the checkbox is checked, I can set the ringer mode to silent and if it's not, I will not change the ringer mode.
Until now I managed to start this permission request when the user clicks the checkbox:
disableSoundCheckboxPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
...
// check for Android N and start the activity mentioned above
But what is the correct way to enable this checkbox only if the permission was granted? I guess it should have something to do with ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
and a listener but I would need some ideas to implement this.
Thanks!