2013-04-01 72 views
4

我用下面的代碼在通知欄中顯示通知。如何在android中添加動態圖像而不是通知圖標?

它工作正常。

但我需要動態顯示來自Web服務的通知圖標。

我該怎麼辦?

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

Notification note = new Notification(R.drawable.image,"status message", System.currentTimeMillis()); 

     Intent in = new Intent(Notify.this,CommentU.class); 
     PendingIntent pi = PendingIntent.getActivity(Notify.this, 0, in, 0); 
     note.setLatestEventInfo(Notify.this,"NotificationTitle", "You have a new Commentio", pi); 
     note.number = ++count; 
     note.vibrate = new long[] { 500l, 200l, 200l, 500l }; 
     note.flags |= Notification.FLAG_AUTO_CANCEL; 
     nm.notify(NOTIFY_ME_ID, note); 

在此先感謝。

+0

看看這個樣本http://code4reference.com/2012/07/android-jelly-bean-notification/ – itsrajesh4uguys

回答

2

我對你有一個建議:你必須下載你想要顯示的圖像,然後你可以將它設置爲位圖:檢查下面的代碼。我創建了一個BITMAP。 其外觀鏈接:

enter image description here

爲此,你必須添加android-support-v4.jar

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("My notification").setLargeIcon(BITMAP) 
       .setContentText("Hello World!"); 

     Intent resultIntent = new Intent(this, test.class); 

     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     stackBuilder.addParentStack(test.class); 

     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     mNotificationManager.notify(NOTIFY_ME_ID, mBuilder.build()); 

爲chekc這個link更多細節。

用戶退出通知單獨或通過使用「全部清除」(如果通知可以被清除):

刪除通知

通知,直到發生以下情況之一仍然可見。

用戶單擊通知,並在創建通知時調用setAutoCancel()。 您可以撥打cancel()來獲得特定的通知ID。此方法還會刪除正在進行的通知。

您致電cancelAll(),刪除您先前發出的所有通知。

編輯:只是取代這一點。

mBuilder = new NotificationCompat.Builder(
       this).setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("My notification").setLargeIcon(BITMAP) 
       .setAutoCancel(true) 
       .setContentText("Hello World!"); 
+0

謝謝您answer.But,我怎麼能消失通知,通知欄時點擊在那。 – user2159475

+0

我用setAutoCanel()方法disaapear。 – user2159475

+0

你必須添加setAutoCanel(true)。檢查我編輯過的答案。 –

0

只需添加一些圖標在您的資源文件夾,然後

int myIcon = R.drawable.image;

你的邏輯到這裏....按照你的邏輯

和圖標的變化值一樣myIcon = R.drawable.somenewimage;

然後最後設置您的通知

Notification note = new Notification(myIcon,"status message", System.currentTimeMillis());