在 android 通知中动态更改图标
Change the icon dynamically in android notification
我的图标有 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.
我的图标有 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.