2015-05-11 23 views
2

我的图标的名称为icon_1,icon_2,icon_3等。我想根据输入动态地更改通知中的图标。输入是范围从1到100的数字。 如果输入为1,则应显示icon_1,如果输入为2,则将显示icon_2等等。是否可以将图标设置在一行中,或者我们被迫使用switch case语句?我在这里粘贴的代码示例更好地理解。切换case case语句肯定会有帮助,但只想知道是否可以在一行中写入以保存100行代码。在android通知中动态更改图标

以下几行代码可能不起作用。但仅仅为了解这些事情,我已经使用过。 输入是变量名称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); 
+1

让您的通知绘制的整数数组并相应地显示它您在收到通知的数量 – WISHY

回答

3

看一看这个

//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.