java - 如何从 URL 图像 Nougat 及以上操作系统版本设置自定义通知图标?

标签 java android android-layout android-notifications custom-notification

我已经通过通知中的图像 URL 实现了自定义图标,这是通过使用以下代码获取默认远程 View 来实现的

RemoteViews contentView = notification.contentView;
                RemoteViews bigContentView = notification.bigContentView;
                int bigIconId = context.getResources().getIdentifier("android:id/big_picture", null, null);
                int iconId = context.getResources().getIdentifier("android:id/icon", null, null);
                int smallIconId = context.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName());
                int largeIconExId = context.getResources().getIdentifier("profile_badge", "id", android.R.class.getPackage().getName());
                if (finalSmallIcon != null)
                    Picasso.with(context).load(finalSmallIcon).into(contentView, iconId, finalData_id, notification);
                if (finalLargeIcon != null) {
                    Picasso.with(context).load(finalLargeIcon).into(contentView, iconId, finalData_id, notification);
                    Picasso.with(context).load(finalLargeIcon).into(contentView, largeIconExId, finalData_id, notification);
                }
                if (finalSmallIcon != null)
                    Picasso.with(context).load(finalSmallIcon).into(contentView, smallIconId, finalData_id, notification);

                if (finalBigPicture != null) {
                    //  Picasso.with(context).load(finalLargeIcon).into(bigContentView, iconId, finalData_id, notification);
                    Picasso.with(context).load(finalBigPicture).into(bigContentView, bigIconId, finalData_id, notification);
                }
            }
        });

但是在 Nougat 及以上版本中,从“notification.contentView”中获取 null。那么我如何使用 customLayout 和 RemoteView 类更新 Nougat 及以上版本中的通知图标。 提前致谢。

最佳答案

    NotificationCompat.Builder notificationBuilder = new notificationCompat.Builder(context);
        notificationBuilder
                .setSmallIcon(context.getApplicationInfo().icon);
        notificationBuilder.setLargeIcon(largeIcon != null &&!largeIcon.isEmpty()? Picasso.with(context).load(largeIcon).get() : BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));

关于java - 如何从 URL 图像 Nougat 及以上操作系统版本设置自定义通知图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068449/

相关文章:

android - 等待和引擎的功能?

android - 将某些文件扩展名关联到我的 android 应用程序

android - Android注解中Main Thread、Ui Thread、WorkerThread、Binder Thread的区别

android - 从不同的 Activity 更改 ImageView

java - 解析插件时出错,您必须为 Maven 存储库指定 URL

JavaFX 按钮在第一次单击时没有响应

Java:Apache Mina IoAcceptor 无法在 Linux 中解除绑定(bind)

java - 在单个 Camel 上下文中分离路由实例

android - 与 Android 设计师合作

android - 嵌入在 Android 应用程序中的网页