我的图标的名称为 icon_1
、icon_2
、icon_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/