在 android 中每年设置重复闹钟

Set repeat alarm for every year in android

我正在 android.Till 中构建警报应用程序,现在我可以为每天、每周和每月创建重复警报。

现在我的要求是每年设置重复闹钟。 TIA

可能这就是答案https://developer.android.com/training/scheduling/alarms.html

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

我想你可以设置明年的闹钟,当时间到了 alarmIntent 运行 并设置年 + 1 的闹钟,这样可以每年重复

// Set the alarm to start at approximately next year
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);

您可以在每年之后设置警报,每次触发警报时您都可以启动一个新的服务意图,该服务意图将在下一年连续设置警报。

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int year = calendar.get(Calendar.YEAR);
calendar.set(Calendar.YEAR, year + 1);

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, alarmIntent);

这将帮助您每年重复报警。如果您正在开发 Alarm App,请使用 DateUtils 这将简化您的工作。

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alrarmTime, DateUtils.YEAR_IN_MILLIS, pendingIntent);