gpt4 book ai didi

android - android中的本地通知立即开始而不是在给定时间

转载 作者:太空狗 更新时间:2023-10-29 16:20:28 26 4
gpt4 key购买 nike

    long when = Calendar.getInstance().getTimeInMillis();
when += 10000;

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
getApplicationContext())
.setWhen(when)
.setContentText(notificationContent)
.setContentTitle(notificationTitle)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(notificationTitle)
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);

这在 onCreate 中运行,但是,我的通知会在应用程序启动时立即创建,而不是在 10 秒后创建。怎么了?

甚至

.setWhen(System.currentTimeMillis()+10000)

10 秒后不显示。直接显示。

最佳答案

来自setWhen(...)...的文档

Add a timestamp pertaining to the notification (usually the time the event occurred). It will be shown in the notification content view by default; use setShowWhen to control this.

setWhen(...) 方法不会为应显示 Notification 的时间设置时间(延迟)。它用于显示某些事件发生的时间。

例如,假设您的应用程序监控接收 SMS 消息 - 当消息到达时您将创建一个 Notification 说“您有一条新的 SMS 消息”并且您将使用 setWhen(。 ..) 以显示收到消息的时间。

如果您想为事件的发生设置特定的延迟或固定时间并显示相关的通知,请使用AlarmManager。 .

关于android - android中的本地通知立即开始而不是在给定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389092/

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