Android: 检测请勿打扰状态?

Android: Detect Do Not Disturb status?

我只是想知道是否有办法检查 Android 设备 (21+) 是否处于“请勿打扰”模式?我知道有 AudioManager.RINGER_MODE_SILENT,但我想知道这是否与这种情况有关,或者是否有更好的检查方法?

我用这个 post 带来了死灵,但我只是在寻找相同问题的解决方案,并且像我一样通过了这个 post,所以我留下了未来的解决方案参考

我找不到 "official" 方法来执行此操作,但确实找到了一个可以 return DnD 状态为整数的值。该功能在内部称为 "zen_mode",因此您可以使用以下代码检查当前值:

Global.getInt(getContentResolver(), "zen_mode")

那就是 return:

  • 0 - 如果 DnD 关闭.
  • 1 - 如果 DnD 开启 - 仅限优先级
  • 2 - 如果 DnD 开启 - 完全静音
  • 3 - 如果 DnD 开启 - 仅警报

当设置为仅优先级时,它return没什么,但是您可以通过假设没有消息 = 优先消息模式来计算该状态。 这必须有是一个错误,因为现在已经一年了,现在 return 就像其他州一样是一个值。不知道它什么时候修复的,但它现在可以正常工作了。

我也尝试过使用设置观察器,它只适用于某些状态转换,所以我认为定期轮询是最好的选择,直到 "official" 监听此状态变化的方法可用。

我在 this Gist 中包含了获取值的观察者和轮询方法。希望它会 help/save 其他人一段时间。

2017 年 3 月 15 日更新: 感谢 David Riha 的评论。添加了 1 - Priority Only 状态来回答,revised the Gist 识别该状态,并输出未知值以记录以防任何其他设备或未来更新决定 return 不同的值。

使用来自 的指针:

SDK 23中,android.app.NotificationManager提供了你需要的接口,即NotificationManager.getCurrentInterruptionFilter().

它应该return其中之一:

  • INTERRUPTION_FILTER_PRIORITY
  • INTERRUPTION_FILTER_ALARMS
  • INTERRUPTION_FILTER_NONE
  • INTERRUPTION_FILTER_ALL
  • INTERRUPTION_FILTER_UNKNOWN

根据Google Help on Nexus Devices 请勿打扰是Android >= 6.0的一个特性,所以SDK 23问问应该是合理的。如果不是,我认为询问原因是合理的,以便能够提供解决方法。

您需要先向清单文件添加更改音频设置的权限。要将铃声模式设置为静音,您必须请求访问通知策略的权限

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

output

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