首页 > 解决方案 > 如何实现 ZenPolicy 规则?

问题描述

这里有人知道 ZenPolicy 以及如何实施规则吗?我正在尝试复制不允许呼叫通过的“请勿打扰”设置,但可能存在例外情况,例如。媒体声音、触摸声音等。我已经设法禁止通话,但是当我(从我的应用程序)切换例外设置时,这将恢复为“仅限已加星标的联系人”。我附上了我试图在我的应用程序中复制的内容的屏幕截图。这是我到目前为止使用的代码:

3 -> {
                                notificationManager.setInterruptionFilter(
                                    NotificationManager.INTERRUPTION_FILTER_PRIORITY
                                )
                                notificationManager.setNotificationPolicy(
                                    NotificationManager.Policy(
                                        PEOPLE_TYPE_NONE,
                                        PRIORITY_SENDERS_ANY,
                                        PRIORITY_SENDERS_ANY
                                    )
                                )
                            }

我也看过:https ://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/service/notification/ZenModeConfig.java;drc=master;l= 754https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/service/notification/ZenPolicy.java

并想出了这个功能,但它什么也没做......:

fun zenPolicyBuilder(): ZenPolicy.Builder {

    val builder = ZenPolicy.Builder()

    val calls = PRIORITY_CATEGORY_CALLS
    val repeatCallers = PRIORITY_CATEGORY_REPEAT_CALLERS
    val alarms = PRIORITY_CATEGORY_ALARMS
    val media = PRIORITY_CATEGORY_MEDIA
    val system = PRIORITY_CATEGORY_SYSTEM
    val events = PRIORITY_CATEGORY_EVENTS
    val reminders = PRIORITY_CATEGORY_REMINDERS

    return when {
        calls != ZenPolicy.PEOPLE_TYPE_UNSET -> {
            builder.allowCalls(PEOPLE_TYPE_NONE)
        }
        repeatCallers != ZenPolicy.STATE_UNSET -> {
            builder.allowRepeatCallers(repeatCallers == ZenPolicy.STATE_ALLOW)
        }
        alarms != ZenPolicy.STATE_UNSET -> {
            builder.allowAlarms(alarms == ZenPolicy.STATE_ALLOW)
        }
        media != ZenPolicy.STATE_UNSET -> {
            builder.allowMedia(media == ZenPolicy.STATE_ALLOW)
        }
        system != ZenPolicy.STATE_UNSET -> {
            builder.allowSystem(system == ZenPolicy.STATE_ALLOW)
        }
        events != ZenPolicy.STATE_UNSET -> {
            builder.allowEvents(events == ZenPolicy.STATE_ALLOW)
        }
        reminders != ZenPolicy.STATE_UNSET -> {
            builder.allowReminders(reminders == ZenPolicy.STATE_ALLOW)
        }
        else -> builder
    }
}

在此处输入图像描述

标签: javaandroidkotlin

解决方案


For anyone who might be interested in future, the first code snippet that I posted actually works fine. The problem was coming from another part of my code which was interfering with this.


推荐阅读