【问题标题】:How to get large icon in NotificationListenerService如何在 NotificationListenerService 中获取大图标
【发布时间】:2016-10-02 22:45:03
【问题描述】:

我有一个类扩展 NotificationListenerService,如何在 onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) 方法中获取大通知图标?

要获得我使用的小图标:

 public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
    extras = sbn.getNotification().extras;
    smallIcon = extras.getInt(Notification.EXTRA_SMALL_ICON, 0);
...
}

它会返回该包中可绘制对象的 id,但我怎样才能获得一个大图标? 我是这样尝试的:

 final Bitmap iconLarge = (Bitmap) extras.getParcelable(Notification.EXTRA_LARGE_ICON);

还有那个

final Bitmap iconLarge = sbn.getNotification().largeIcon;

它总是返回 null,我的目标是 API 21+

sbn.getNotification().getLargeIcon()

不工作,只有 23 岁以上。 有什么帮助吗?

【问题讨论】:

  • 你解决了吗?我有同样的问题,largeIcon 已被弃用。
  • 是的,我添加了一个答案,检查一下。

标签: android notifications icons


【解决方案1】:

根据 Android 文档,

getLargeIcon() 方法仅在 API 级别 23 中添加。

这就是为什么当您尝试在 Android Lollipop(API 级别 21)中访问 API 并在 Marshmallow(API 级别 23)的情况下正常工作时,API 返回您的原因

【讨论】:

    【解决方案2】:

    已解决:

    Bundle extras = sbn.getNotification().extras;
    Bitmap bigIcon = (Bitmap) extras.get(Notification.EXTRA_LARGE_ICON);
    

    【讨论】:

    • 非常感谢。我试试看:)
    【解决方案3】:

    试试:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
       (bundle["android.largeIcon"] as Icon).loadDrawable(baseContext).toBitmap()
     } else {
        bundle.get(Notification.EXTRA_LARGE_ICON) as Bitmap
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      相关资源
      最近更新 更多