2010-11-03 116 views
13

我試圖使用Android的通知管理器創建通知,但是,訣竅是我希望通知在未來30天內顯示。在我的代碼我這樣做:Android延遲通知

Intent notificationIntent = new Intent(this, MyClass.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
long when = System.currentTimeMillis() + (30 * 24 * 3600 * 1000); 
Notification notification = new Notification(R.drawable.some_image, "A title", when); 
notification.setLatestEventInfo(getApplicationContext(), "You're late", "Some description", contentIntent); 
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
nm.notify(NOTIFY_ATTEND_ID, notification); 

但是,該通知仍呈現瞬間可達。從我讀的內容來看,Notification構造函數的「when」參數僅用於對StatusBar中的通知進行排序。無論如何要在以後的日期/時間顯示通知嗎?提前致謝。

+0

將它展現在未來30天不是微不足道的。警報等在啓動時死亡。你必須保持服務或東西 – Falmarri 2010-11-03 06:05:19

回答

4

有沒有辦法讓通知顯示在未來的日期/時間?

由於Falmarri建議,您將需要處理這個自己,雖然我跟他的做法不以爲然。您將需要使用AlarmManager。不過,我很懷疑AlarmManager會持續30天的工作時間,儘管您可以嘗試。您可能需要使用AlarmManager進行每日/每週的任務,以通過單獨的警報安排當天/周的通知。正如Falmarri所建議的那樣,您還需要在重新啓動時重新組織這個警報花名冊,因爲他們會被抹去。

+0

感謝您的建議下議院。我會看看AlarmManager,看看它是否會起作用。由於我也在這個應用程序中運行SQLite數據庫,因此我可以在其中存儲一些通知信息。我會讓你們知道它是怎麼回事。 – Jeff 2010-11-04 03:32:17

+0

再次感謝法爾瑪利和CommonsWare。你的解決方案爲我工作。我結束了以下AlarmController API演示:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmController.html。 – Jeff 2010-11-05 15:11:57