在android中设置每年的重复警报

问题描述 投票:1回答:3

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

现在我的要求是每年设置重复警报。 TIA

android alarmmanager alarm android-alarms repeatingalarm
3个回答
1
投票

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

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

0
投票

可能这是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);

0
投票

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

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alrarmTime, DateUtils.YEAR_IN_MILLIS, pendingIntent);
© www.soinside.com 2019 - 2024. All rights reserved.