【问题标题】:How to get color of notification for Android?如何获取 Android 通知的颜色?
【发布时间】:2016-04-15 22:32:16
【问题描述】:

某些通知的小通知图标在大图标的左下角显示为徽章(请参阅第一个、第二个和最后一个通知)。

但是,在 Lollipop 之后,操作系统会自动为小图标设置白色滤镜,每当我尝试从我的 NotificationListenerService 获取它们以进行显示时,它们都会以白色返回。有什么方法可以提取小图标的背景颜色,或者直接将小图标作为彩色绘图获取?下面是我的代码,它只返回完全白色的图标。

public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);

    Bundle extras = sbn.getNotification().extras;
    int smallNotiIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);

    //Code to display obtained small icon in an imageView shows up as white
}

【问题讨论】:

    标签: android android-5.0-lollipop android-notifications android-icons


    【解决方案1】:

    可以通过color公共字段从通知中检索颜色:

    Notification notification = sbn.getNotification();
    // ARGB color set with the notification
    int color = notification.color;
    

    还有COLOR_DEFAULT作为通知的默认颜色,您可以使用它来查看是否设置了自定义颜色。

    【讨论】:

    • 我不知道为什么我之前没有在文档中看到这个。非常感谢您花时间回答这个问题。你提到的效果很好。
    猜你喜欢
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多