2011-11-27 89 views
1

我正在使用一個AsyncTask,它下載一個apk文件,然後以後執行方法安裝它。Android存儲多個通知ID並允許同時顯示多個通知

預執行 {

Get a notification variable from shared preference and adds +1 to it 
Create a new notification based on this variable 

}

做背景 {

Update this notification based on its ID -1 (to refer to initial notification ID) 

}

後執行 {

Update this notification based on its ID -1 (to refer to initial notification ID) 

}

同時該線程運行時(例如下載網絡上非常大的文件),我將允許用戶啓動另一個下載時。在這種情況下,我想顯示2個通知,而不是重複使用相同的通知ID。當我在後臺執行和後臺執行線程中引用其通知ID時,具有兩個唯一通知ID的最佳方式是什麼?謝謝

回答

2

小睡後休息,找到解決方案。那一定是太累了,看着辦吧

我已經放在一個局部變量private int notification_ID每個AsyncTask
內創建通知後,插入共享偏好(Notification_ID + 1)
做背景,並執行後仍然使用這個私有變量在更新此通知時

當一個新的AsyncTask線程啓動時,它將首先從共享首選項讀取,這將是一個新值(假設第一個爲0,第二個爲0 + 1 = 1)在後臺執行和後執行嘗試訪問其notification_ID它將訪問自己的私人變種因此值爲1因此不會與第一次或後續通知衝突