显示从某个开始日期到某个结束日期的重复通知

Show repeating Notification from some start Date until some end date

我有许多每周重复的活动,每个活动都有一个 start/End 日期。 它应该每周在同一天和同一时间出现。

所以我想在每周的特定日期和时间显示每个事件的通知,从开始日期到结束日期。

任何人都可以向我提供有关如何实现此功能的教程/解释/链接吗?

查看官方 Android 文档 - link here.

您需要使用AlarmManager中的setRepeating函数来实现。创建一个 BroadcastReceiver,在收到 Intent 时创建一个通知。将此 Intent 设置为 PendingIntent 并将 PendingIntent 赋予 AlarmManager#setRepeating

示例代码 -

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);