【问题标题】:NotificationListenerService get Notification Icon?NotificationListenerService 获取通知图标?
【发布时间】:2013-08-05 07:50:32
【问题描述】:

在扩展新的(SDK18,JB-4.3)NotificationListenerService 的服务中,我想获取通知的状态栏图标。

mStatusBarNotification.getNotification().icon 返回状态栏drawable 的资源ID,但该资源ID 自然不在我的应用程序的范围/资源内。还有mStatusBarNotification.getNotification().largeIcon(返回Bitmap),但不是为所有通知设置它并返回“错误”图标(展开的通知抽屉中的图像)。

【问题讨论】:

    标签: android notifications android-4.3-jelly-bean


    【解决方案1】:

    StatusBarNotification 上使用getPackageName() 找出发布Notification 的应用程序。然后您可以use createPackageContext() 获取该软件包的Context,然后使用该Context 检索图像(例如,通过getResources())。

    【讨论】:

    • 它是如何工作的? setSmallIcon 仍然需要一个 int 资源 ID,并且使用 createPackageContext 和 getResources 只是让我可以访问 Drawable 本身。
    • @KyleJahnke:我不知道与这个问题有任何关系的setSmallIcon() 方法。也许您正在寻找创建一个Notification;这个问题不是关于创建一个Notification,而是检查一个现有的。
    • 他在问如何找到另一个通知输入的图标进入 setSmallIcon(); notification.extras.getInt(Notification.EXTRA_SMALL_ICON)
    • @CommonsWare 这没有提供正确的答案。
    • @NoumanCh:您在回答中使用了我提供的技术 (createPackageContext())。
    【解决方案2】:

    这是替代解决方法。

    我们可以从sbn.getNotification().extras.getInt("android.icon")获取drawable,然后使用customview在通知中显示这个drawable。

    这是使用 android.icon 值获取 Drawable 的方法:

                RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_push_notification);
                contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
                contentView.setTextViewText(R.id.title, notificationModel.getTitle());
                contentView.setTextViewText(R.id.text, notificationModel.getBody());
               
    
                try {
                       //now get the context of other app and then get drawable from resoures
                        Drawable drawable1 = context.createPackageContext(notificationModel.getPackageName(), CONTEXT_IGNORE_SECURITY).getDrawable(notificationModel.getIcon());
                        Bitmap bitmap = drawableToBitmap(drawable1);
                        contentView.setImageViewBitmap(R.id.image, bitmap);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
    

    我的notificationModel 是:

    notificationModel.setKey(sbn.getId());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多