【发布时间】:2015-10-01 22:33:27
【问题描述】:
我只是想知道是否有办法检查 Android 设备 (21+) 是否处于请勿打扰模式?我知道有AudioManager.RINGER_MODE_SILENT,但我想知道这是否与这种情况有关,或者是否有更好的检查方法?
【问题讨论】:
标签: android settings android-5.0-lollipop volume
我只是想知道是否有办法检查 Android 设备 (21+) 是否处于请勿打扰模式?我知道有AudioManager.RINGER_MODE_SILENT,但我想知道这是否与这种情况有关,或者是否有更好的检查方法?
【问题讨论】:
标签: android settings android-5.0-lollipop volume
我在这篇文章中带来了 necro,但我只是在寻找解决同一问题的方法,并且像我一样通过了这篇文章,所以我为将来的参考留下了一个解决方案。
我找不到执行此操作的“官方”方法,但确实找到了一个可以将 DnD 状态作为整数返回的值。该功能在内部称为“zen_mode”,因此您可以使用以下代码检查当前值:
Global.getInt(getContentResolver(), "zen_mode")
这会返回:
当设置为仅优先级时,它不会返回任何内容,但您可以通过假设 no news = Priority Messages 模式来确定该状态。 这一定是一个错误,因为现在已经过去一年了,这现在返回一个值,就像其他状态一样。不知道什么时候修复的,但现在可以正常工作了。
我也尝试了设置观察器,它只适用于某些状态转换,所以我认为定期轮询是最好的选择,直到有一种“官方”的方式来监听这种状态变化。
我已经包含了在this Gist 中获取值的观察者和轮询方法。希望它能帮助/节省其他人一些时间。
2017 年 3 月 15 日更新:感谢 David Riha 的评论。添加了1 - Priority Only 状态以回答,revised the Gist 以识别该状态,并在任何其他设备或未来更新决定返回不同值时输出未知值以进行日志记录。
【讨论】:
来自Android how to turn on do not disturb (dnd) programmatically的指针:
在SDK 23中,android.app.NotificationManager提供了你需要的接口,即NotificationManager.getCurrentInterruptionFilter()。
它应该返回以下之一:
INTERRUPTION_FILTER_PRIORITYINTERRUPTION_FILTER_ALARMSINTERRUPTION_FILTER_NONEINTERRUPTION_FILTER_ALLINTERRUPTION_FILTER_UNKNOWN根据Google Help on Nexus Devices 请勿打扰 是 Android >= 6.0 的一个功能,所以 SDK 23 应该是合理的要求。如果不是,我认为有理由问为什么,以便能够提供解决方法。
【讨论】:
INTERRUPTION_FILTER_ALL。我不知道休息时它会受到影响,如果有的话。
您需要先在清单文件中添加更改音频设置的权限。为了能够将振铃模式设置为静音,您必须请求访问通知策略的权限
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后要检测“请勿打扰”,您可以执行以下操作
NotificationManager notificationManager = (NotificationManager) getActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new
Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
return;
}
ToggleDoNotDisturb(notificationManager);
private void ToggleDoNotDisturb(NotificationManager notificationManager) {
if (notificationManager.getCurrentInterruptionFilter() == NotificationManager.INTERRUPTION_FILTER_ALL) {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
audioToggle.setText(R.string.fa_volume_mute);
} else {
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
audioToggle.setText(R.string.fa_volume_up);
}
}
你还需要检查权限
NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
【讨论】:
NotificationManager.Policy a = null;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
a = mNotificationManager.getNotificationPolicy();
Log.d(TAG, "onClickDND: "+a.priorityCallSenders);
Log.d(TAG, "onClickDND: "+a.priorityCategories);
}
输出
--------------------------------------
FROM ANYONE
call senders : 0
categories : 109
Message : 0
State : 1
FROM CONTACTS ONLY
call senders : 1
categories : 109
Message : 1
State : 1
FROM STARRED CONTACTS ONLY
call senders : 2
categories : 109
Message : 2
State : 1
NONE
call senders : 2
categories : 97
Message : 2
State : 1
【讨论】: