【发布时间】:2012-10-10 07:12:26
【问题描述】:
我有一个 for 块,如下所示:
for(int counter = 0; counter < sList.size(); counter++){
String s = sList.get(counter);
Notification notification = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText(s).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(counter, notification);
}
此块位于由警报管理器触发的服务中。因此,在用户看到通知之前,这个块很可能会被执行几次。 当在 sList 中添加某些内容时重新执行此块时,它会覆盖当前通知,因为通知的 ID 相同。 我怎样才能防止这种情况发生?我怎样才能每次都获得一个唯一的 ID?或者是否有可能避免整个 ID 部分,比如告诉 android 无论如何都必须显示通知,无论 ID 是什么?
提前致谢!
【问题讨论】:
-
这是一个简单的方法,同时确保 Id 的唯一性:stackoverflow.com/a/58786832/8697587
标签: java android notifications