android 中 setNotificationPolicy 和 setInterruptionFilter 的区别

Difference between setNotificationPolicy and setInterruptionFilter in android

android中的NotificationManagerclass列出了两个方法setInterruptionFilter (int interruptionFilter)setNotificationPolicy (NotificationManager.Policy policy).

来自文档:

setInterruptionFilter

public final void setInterruptionFilter (int interruptionFilter)

Sets the current notification interruption filter.

The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.

setNotificationPolicy

public void setNotificationPolicy (NotificationManager.Policy policy)

Sets the current notification policy.

两者都是在 API 级别 23 中添加的。 根据我的理解,两者似乎都完成了为 android 设备设置“请勿打扰”策略的相同任务。这两种方法到底有什么区别?

使用 setNotificationPolicy 您可以设置请勿打扰 "Allow Interruptions" 策略,例如 NotificationManager.Policy.PRIORITY_CATEGORY_ALARM(允许警报)

使用 setInterruptionFilter 您可以设置实际的“请勿打扰”开启或关闭,其中:

NotificationManager.INTERRUPTION_FILTER_PRIORITY = 使用 setNotificationPolicy 设置开启 DND

NotificationManager.INTERRUPTION_FILTER_NONE = 为每次打扰设置免打扰

NotificationManager.INTERRUPTION_FILTER_ALL = 关闭免打扰

提示。您可以先获取策略,然后使用 NotificationManager.getNotificationPolicy() 更改它,以便在完成后将其恢复。