Android:设置“请勿打扰"(setInterruptionFilter) 适用于除应用程序音频流之外的所有通知 [英] Android: set "do not disturb" (setInterruptionFilter) for all notifications besides the apps audio stream

查看:87
本文介绍了Android:设置“请勿打扰"(setInterruptionFilter) 适用于除应用程序音频流之外的所有通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个播放音频的应用程序,在播放过程中不得中断,因为它向其他设备提供时间码信息,并且当设备播放不同的内容(例如电话)时将无法正常工作.

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

我可以通过以下方式激活请勿打扰"(DND)模式

I can activate the "do not disturb" (DND) mode via

NotificationManager.setInterruptionFilter(
    NotificationManager.INTERRUPTION_FILTER_NONE)

但是 这也会使我的应用程序的音频流静音.(请参阅1)

but that will mute the audio-stream of my app also. (see 1)

仅当用户具有 root 访问权限时才可以阻止电话呼叫.我可以询问我的用户,也不会将其他通知静音

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

我可以使用飞行模式,因为该应用需要 WiFi 才能通过 NTP 从互联网上查询时间.

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?

所以为了缩短它,我需要做的是:

用户开始音频 -> 用户被询问是否要激活 dnd 以不中断音频播放 ->音频播放开始 ->用户停止音频 -> DND 已停用

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

提前感谢您的想法!

推荐答案

您基本上是通过 INTERRUPTION_FILTER_NONE 禁用所有声音.使用 INTERRUPTION_FILTER_PRIORITY 并配置通知策略以允许媒体播放,如下所示:

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

您还可以通过向 setNotificationPolicy 添加附加参数来抑制某些效果,例如灯光 - 有关详细信息,请参阅 Android 帮助.

You can also suppress some effects like lights by adding additional parameter to setNotificationPolicy – see Android help for details.

这篇关于Android:设置“请勿打扰"(setInterruptionFilter) 适用于除应用程序音频流之外的所有通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆