gpt4 book ai didi

java - Android:获取通知的唯一ID

转载 作者:太空狗 更新时间:2023-10-29 23:00:24 26 4
gpt4 key购买 nike

我有一个 for block ,看起来像这样:

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);
}

此 block 位于由警报管理器触发的服务中。因此,在用户看到通知之前,这个 block 可能真的会执行几次。当这个 block 被添加到 sList 时重新执行时,它会覆盖当前的通知,因为通知的 ID 是相同的。我怎样才能防止这种情况发生?我怎样才能每次都获得唯一的ID?或者是否有可能避免整个 ID 部分,比如告诉 android 无论如何都必须显示通知,无论 ID 是什么?

提前致谢!

最佳答案

long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.valueOf(last4Str);

notificationManager.notify(notificationId, notif);

获取当前系统时间。然后我只读取它的最后 4 位数字。每次显示通知时,我都使用它来创建唯一 ID。这样可以避免获取相同或重置通知ID的可能性。

关于java - Android:获取通知的唯一ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12978184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com