2015-04-30 25 views
0

我想要根据输入的日期(生日)每年出现通知。我有其他一切工作栏,每年如何设置通知。正如你可以在下面看到的,我已经改变了代码在间隔时间去说“这里”。还有几天的时间间隔,我知道我可以乘上365,但如果发生什么闰年..AlarmManager - 如何将通知设置为每年出现

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

请记住,每次设备重新启动时都需要重新创建警报,这一切都将在一年后得到保证。因此您需要创建一个'BroadcastReceiver'来侦听ACTION_BOOT_COMPLETED行为(并且您需要'RECEIVE_BOOT_COMPLETED'权限),并在每次启动时重置警报。 –

回答

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); 
     } 
    } 
} 
+0

我仍然无法弄清楚这一点,但我知道你的意思!你能否进一步解释甚至给我一些示例代码? @ user3829751 – Garth

+0

已编辑,添加了代码示例,供您复制和粘贴乐趣:) – user3829751

0

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

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

3)单日的快讯/今天的自我。

另外,您也可以显示在生日下周/月等