2013-05-05 33 views
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秒後創建。 什麼錯了?在Android的本地通知立即啓動,而不是在給定的時間

即使

.setWhen(System.currentTimeMillis()+10000) 

不使其顯示10秒後。它直接顯示。

回答

5

setWhen(...)文檔...

添加有關通知(通常是事件發生的時間)時間戳。它將默認顯示在通知內容視圖中;使用setShowWhen來控制它。

setWhen(...)方法不設定時,應顯示的Notification的時間(延遲)。它用來顯示某個事件發生的時間。例如,假設您的應用程序監視器正在接收SMS消息 - 當消息到達時,您將創建一條Notification,表示「您有新的SMS消息」,並且您將使用setWhen(...)來顯示收到消息的時間。

如果您想要設置某個事件發生的特定延遲或固定時間,並且要顯示關聯的Notification,請使用AlarmManager