AlarmManager - 如何设置每年出现的通知

时间:2015-04-30 17:53:15

标签: android notifications alarmmanager date-of-birth

我希望每年都会显示一个通知,具体取决于输入的日期(生日)。我还有其他一切工作,如何每年设置一个通知。正如您在下面看到的,我已经将代码更改为“HERE”,其中间隔为。有几天的间隔,我知道我可以乘以365.但如果它是闰年会发生什么..

int REQUEST_CODE = 7;
Intent intent = new Intent(Activity2.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Activity2.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, System.currentTimeMillis(), HERE, pendingIntent);

2 个答案:

答案 0 :(得分:1)

你可以替换这里'使用一种方法确定从今天起的今年2月是否处于闰年,然后根据这些检查返回值365或366天(以毫秒为单位)。

private long millisUntilNextYear(){

    //Set days in a year for Leap and Regular
    final int daysInLeapYear = 366;
    final int daysInYear = 365;

    //Get calendar instance
    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    //Get this year and next year
    int thisYear = cal.get(GregorianCalendar.YEAR);
    int nextYear = thisYear + 1;

    //Get today's month
    int thisMonth = cal.get(GregorianCalendar.MONTH);

    //Get today's date
    int dayOfMonth = cal.get(GregorianCalendar.DAY_OF_MONTH);

    //Is today before February? If so then the following February is in THIS year
    if (thisMonth < GregorianCalendar.FEBRUARY){

        //Check if THIS year is leapYear, and return correct days (converted to millis)
        return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
    }

    //Is today after February? If so then the following February is NEXT year
    else if (thisMonth > GregorianCalendar.FEBRUARY) {
        //Check if NEXT year is leapYear, and return correct days (converted to millis)
        return cal.isLeapYear(nextYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
    }

    //Then today must be February.
    else {
        //Special case: today is February 29
        if (dayOfMonth == 29){
            return daysToMillis(daysInYear);
        } else {
            //Check if THIS year is leapYear, and return correct days (converted to millis)
            return cal.isLeapYear(thisYear) ? daysToMillis(daysInLeapYear) : daysToMillis(daysInYear);
        }
    }
}

答案 1 :(得分:0)

1)以MM / DD / YY格式保存日期。

2)在您打开应用程序时(或在不同时间)阅读这些日期

3)设置单日/今天的警报。

另外,您还可以显示下周/下周的生日等。