2015-05-11 22 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.