簡體   English   中英

在Android通知中動態更改圖標

[英]Change the icon dynamically in android notification

我的圖標的名稱為icon_1icon_2icon_3等。 我想根據輸入動態更改通知中的圖標。 輸入的數字范圍是1到100。如果輸入為1,則應顯示icon_1 ,如果輸入為2,則將顯示icon_2 ,依此類推。 是否可以將圖標設置為一行,還是我們不得不使用switch case語句? 我粘貼在此處的代碼示例可以更好地理解。 切換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. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM