Android:检测“请勿打扰"状态吗? [英] Android: Detect Do Not Disturb status?
问题描述
我只是想知道是否可以检查 Android设备(21 +)是否处于免打扰模式?我知道有AudioManager.RINGER_MODE_SILENT
,但是我想知道这是否与这种情况有关,或者是否有更好的检查方法?
I was just wondering if there's a way to check if an Android device (21+) is in Do Not Disturb mode? I know there's AudioManager.RINGER_MODE_SILENT
, but I was wondering if that pertains to this situation, or if there's a better way to check?
推荐答案
我为这篇文章带来了坏处,但是我只是在寻找解决同一问题的方法,并且像我一样走过了这篇文章,所以我离开了将来参考的解决方案.
I'm bringing the necro with this post, but was just searching for a solution to the same problem and came past this post as I did, so am leaving a solution for future ref.
我找不到执行此操作的官方"方法,但确实找到了可以将DnD状态返回为整数的值.该功能在内部称为"zen_mode",因此您可以使用以下代码检查当前值:
I couldn't find an "official" way to do this, but did find a value that can return the DnD state as an integer. The feature is called "zen_mode" internally, so you can check the current value with the following code:
Global.getInt(getContentResolver(), "zen_mode")
那将返回:
- 0 -如果DnD为 off .
- 1 -如果DnD是 on -仅优先级
- 2 -如果DnD为 on -完全静音
- 3 -如果DnD已打开 -仅警报
- 0 - If DnD is off.
- 1 - If DnD is on - Priority Only
- 2 - If DnD is on - Total Silence
- 3 - If DnD is on - Alarms Only
当该设置仅是优先级时,它什么也不返回,但是您可以通过假设没有新闻= Priority Messages模式来弄清楚该状态.这肯定是一个错误,因为现在已经有一年了,它现在会返回一个值,就像其他状态一样.不知道何时修复它,但它现在可以按您期望的那样工作.
When the setting is priority only, it returns nothing, but you can figure out that state by assuming no news = Priority Messages mode. This must have been a bug, as now it's a year on, this now returns a value just like the other states. No idea when it was fixed, but it now works as you'd expect.
我也尝试过设置观察器,该观察器仅对某些状态转换有效,因此我认为定期轮询是最好的选择,直到可以使用官方"方式监听此状态变化为止.
I tried with a settings observer too, which works but only for certain state transitions, so I think polling periodically is the best bet, until an "official" way to listen for this state change becomes available.
我在此要点中包括了获取值的观察者和轮询方法.希望它能帮助/节省一些时间.
I've included both observer and polling methods of getting the value in this Gist. Hopefully it'll help/save someone else some time.
更新2017年3月15日:感谢 David Riha 的评论.添加了1 - Priority Only
状态来回答,修改了要点以识别该状态并输出未知值,以便在其他设备或将来的更新决定返回其他值时进行记录.
Update 15th March 2017: Thanks to David Riha for the comment. Added 1 - Priority Only
state to answer, revised the Gist to recognise that state, and to output unknown values to log in case any other devices or future updates decide to return a different value.
这篇关于Android:检测“请勿打扰"状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!