首页 > 解决方案 > 在 Android P 和 Q Beta 中以编程方式切换异常时出现意外行为

问题描述

我有一个非常简单的应用程序,只有一个按钮。当我按下此按钮时,我希望以编程方式切换“仅来自已加星标的联系人”和“播放媒体声音”。我正在使用的代码是:

private void setPolicyStarredContacts() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    assert notificationManager != null;
    notificationManager.setNotificationPolicy(
            new NotificationManager.Policy(PRIORITY_CATEGORY_MEDIA,
                    NotificationManager.Policy.PRIORITY_SENDERS_STARRED,
                    NotificationManager.Policy.PRIORITY_SENDERS_ANY));

}

实际发生的是只有“播放媒体声音”被激活,而不是其他选项。对于我做错了什么的一些迹象,我将不胜感激。干杯!

标签: javaandroidandroid-notificationsandroid-9.0-pieandroid-10.0

解决方案


Ok, I have actually found the answer! Here it is for future searchers:

private void setPolicyStarredContacts() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    assert notificationManager != null;
    notificationManager.setNotificationPolicy(
            new NotificationManager.Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_MEDIA,
                    NotificationManager.Policy.PRIORITY_SENDERS_STARRED,
                    NotificationManager.Policy.PRIORITY_SENDERS_ANY));

}

推荐阅读