2010-08-12 94 views
9

我想在我的應用程序中創建多個通知。爲了唯一標識每個通知,我給了他們一個唯一的身份標識。以下是我的代碼:Android:管理多個通知

private void updateNotification(int notificationId, int clockStatusID, CharSequence text) { 
//notificationManager.cancel(notificationId); 
// throws up an ongoing notification that the timer is running 
Log.i("TIMERCOUNT", "Notification id: " + notificationId); 
Notification not = new Notification(clockStatusID, // the 
    // icon 
    // for 
    // the 
    // status 
    // bar 
    text, // the text to display in the ticker 
    System.currentTimeMillis() // the timestamp for the 
    // notification to appear 
); 
Intent intent = new Intent(); 
intent.putExtra("notificationID", notificationId); 
intent.setAction("actionstring" + System.currentTimeMillis()); 
intent.setClassName("com.chander.time.android.activities", 
"com.chander.time.android.activities.Tabs"); 


not.setLatestEventInfo(self, 
    getText(R.string.timer_notification_title), 
    getText(R.string.timer_on_notification_text), PendingIntent 
    .getActivity(this, 0, intent, 
     PendingIntent.FLAG_UPDATE_CURRENT)); 

not.flags += Notification.FLAG_ONGOING_EVENT; 
not.flags += Notification.FLAG_NO_CLEAR; 
notificationManager.notify(notificationId, not); 
} 

問題: 當選擇了一個通知,標籤活動被稱爲合格的意圖。我想要訪問在選項卡中選擇的通知的唯一通知ID。我嘗試了intent.putExtra()來保存意圖中的notificationId。但是,對於多個通知,它將覆蓋notificationId並返回最新的通知。我不明白爲什麼會發生這種情況,我如何避免這種覆蓋notificationId。

感謝, 錢德爾

回答

15

我得到了答案。的意圖都拿到緩存中,創建一個新的意圖,只需添加以下代碼塊:

saveCallIntent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); 

這使得意圖是獨一無二的。

此外,有人建議我下面的一段代碼,以使意圖獨特:

saveCallIntent.setAction("actionstring" + System.currentTimeMillis()); 

它沒有幫我,但可能會有所幫助別人。

--Chander

7

只需將notificationId,而不是0 pendingIntent

PendingIntent.getActivity(this, notificationId, intent,PendingIntent.FLAG_UPDATE_CURRENT)); 

用戶還必須更新以下代碼以及上面的代碼以使其工作。

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