Reputation: 401
I have built an app that plays audio which must not be interrupted during it plays, because it provides timecode-information to other devices, and will not work properly when the device plays something different in between (e.g. a phone call).
So I need a solution to mute notifications while playing audio. I found out so far:
When setting the permissions previously and guiding the user to allow the app to turn on DND with
startActivityForResult(new
Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);
I can activate the "do not disturb" (DND) mode via
NotificationManager.setInterruptionFilter(
NotificationManager.INTERRUPTION_FILTER_NONE)
but that will mute the audio-stream of my app also. (see 1)
block phone calls is only possible when the user has root access. I can not ask my users for that and also it will not mute other notifications
I can not use the flight mode, because the app needs WiFi to query the time via NTP from the internet.
Probably a solution is, to set the DND-mode manually and start the app and the stream afterwards. But how is it possible to send the user directly to the DND-menu, so that he/she could enable DND-mode and return to the app by pressing the back button? I only found the Settings-Actions:
ACTION_NOTIFICATION_LISTENER_SETTINGS
ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
ACTION_SOUND_SETTINGS
which are not the right screens for that. Maybe it is provided somehow to open the quick settings of the DND mode?
So to shorten it up, what I need to do:
User starts audio -> user is asked if he wants to active dnd for no interuption of the audio-playback -> audio-playback is started -> user stops audio -> DND is deactivated
Thanks in advance for ideas on that!
Upvotes: 2
Views: 3697
Reputation: 1529
And you also need to add this permission
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
and call this on your Activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NotificationManager n = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if(n.isNotificationPolicyAccessGranted()) {
} else {
startActivityForResult(new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);
}
}
Upvotes: 0
Reputation: 993
In Kotlin this can be done as..
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val policy =
NotificationManager.Policy(Policy.PRIORITY_CATEGORY_MEDIA, 0, 0)
notificationManager.notificationPolicy = policy
}
NOTE: This is only possible in Android API level 28 and above since the policy PRIORITY_CATEGORY_MEDIA was added in that API.
There are other Policy categories that might be helpful in other cases. like PRIORITY_CATEGORY_REMINDERS, PRIORITY_CATEGORY_EVENTS, PRIORITY_CATEGORY_MESSAGES, PRIORITY_CATEGORY_CALLS, PRIORITY_CATEGORY_REPEAT_CALLERS etc.. read more about them here NotificationManager.Policy Official doc
Upvotes: 0
Reputation: 181
You are basically disabling all sounds with INTERRUPTION_FILTER_NONE
. Use INTERRUPTION_FILTER_PRIORITY
and configure notification policy to allow media playback like the following:
NotificationManager.setInterruptionFilter(
NotificationManager.INTERRUPTION_FILTER_PRIORITY );
NotificationManager.setNotificationPolicy(
new NotificationManager.Policy( NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA,
0, 0 ) );
You can also suppress some effects like lights by adding additional parameter to setNotificationPolicy
– see Android help for details.
Upvotes: 2