2
    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 秒后不显示。它直接显示。

4

1 回答 1

6

From the documentation for 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.

The setWhen(...) method doesn't set a time (delay) for when a Notification should be displayed. It's used to show the time that some event has occured.

For example, suppose your app monitors receiving SMS messages - when a message arrives you would create a Notification saying "You have a new SMS message" and you'd use setWhen(...) to show the time that the message was received.

If you want to set a particular delay or fixed time for an event to occur and an associated Notification to be shown, use AlarmManager.

于 2013-05-05T21:46:48.270 回答