在Android P和Q Beta中以编程方式切换异常时的意外行为

时间:2019-04-16 14:23:27

标签: java android android-notifications android-9.0-pie androidq

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

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

}

实际发生的是仅激活了“播放媒体声音”,而没有激活其他选项。我将为我做错了事表示感激。干杯!

1 个答案:

答案 0 :(得分:0)

好的,我已经找到答案了!这是供将来的搜索者使用的:

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

}