在安卓中为每年设置重复闹钟



我正在用安卓构建一个警报应用程序。到目前为止,我可以每天,每周和每个月创建重复警报。

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

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

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

这将帮助您每年重复闹钟。使用DateUtils,如果您正在使用警报应用程序,这将简化您的工作。

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

也许这就是答案 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);

我认为你可以为明年设置闹钟,当时间到来时,闹钟意图运行并为年份 + 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);

相关内容

  • 没有找到相关文章

最新更新