安卓延迟通知

13

我正在尝试使用Android的通知管理器创建通知,但关键是我希望通知在未来30天内显示。我的代码如下:

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

然而,通知仍然立即显示出来。据我所读,“when”参数只用于在状态栏中排序通知的Notification构造函数中。有没有办法让通知在未来的某个日期/时间显示?提前谢谢。


让它在未来30天内显示并不容易。警报等在启动时会被关闭。你需要保持一个服务或类似的东西。 - Falmarri
1个回答

4

有没有办法让通知在将来的某个日期/时间显示出来?

没有。

正如Falmarri所建议的那样,您需要自己处理此问题,尽管我不同意他的方法。您需要使用AlarmManager。但是,我怀疑AlarmManager在30天的持续时间内是否有效,不过您可以尝试一下。您可能需要使用AlarmManager来安排每日/每周任务,通过单独的提醒来安排当天/当周的通知。您还需要在重新启动时重新组合这些提醒的列表,因为它们会被清除,就像Falmarri所建议的那样。


谢谢你的建议,Commons。我会看看AlarmManager,看看它是否能解决问题。由于我还在这个应用程序中运行一个SQLite数据库,我可以将一些通知信息存储在那里。我会让大家知道进展情况的。 - Jeff
再次感谢Falmarri和CommonsWare。你们的解决方案对我非常有用。我最终遵循了AlarmController API Demo: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmController.html。 - Jeff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接