android - 在android通知中动态更改图标

标签 android android-notifications

我的图标的名称为 icon_1icon_2icon_3 等。我想根据输入动态更改通知中的图标。输入是1到100之间的数字。 如果输入为 1,则应显示 icon_1,如果输入为 2,则应显示 icon_2,依此类推。是否可以在一行中设置图标,或者我们被迫使用 switch case 语句?我将示例代码粘贴到此处以便更好地理解。 switch case 语句肯定会有帮助,但只想知道是否可以写一行来节省 100 行代码。

以下代码行可能不起作用。但只是为了了解这些东西,我已经使用了。 输入是num变量名中的数字。

Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n  = new Notification.Builder(this)
    .setContentText("Subject")
    .setSmallIcon(R.drawable.icon_+"num")  //Here is the doubt..How can we modify this line to work
    .setContentIntent(pIntent)
    .setAutoCancel(true)
     .build();
NotificationManager notificationManager=NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n); 

最佳答案

看看这个

//create a array of your notification icons
int[] not_icon={R.drawable.icon_1,R.drawable.icon_2,R.drawable.icon_3.......so on};

//pass the array accordingly to your input or payload
.setSmallIcon(not_icon[3]);  //3 is the number you received in your payload. 

关于android - 在android通知中动态更改图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30160714/

相关文章:

android - whatsapp/telegram 如何确保来电通知?

android - 如何为 API 级别 16 以上的所有设备发送本地通知?

Android:有些通知可以取消,有些不可以

android - 省略号选项的含义

Android Java AsyncTask isCancelled 问题

android - 默认 Android 12 相机,对所有文件没有访问管理 ResultCode 始终为 0

android - 在 android studio 中启动服务时出现 startForeground 错误的错误通知

android - PendingIntent.getBroadcast lint 警告

c# - Monodroid BitmapFactory.DecodeFileDescriptor 位图始终为 null

android - 使用 NDK 时如何正确命名原生函数