2011-11-27 93 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因此不会与第一次或后续通知冲突