2014-06-23 81 views
4

我有一个特定的要求来创建将在特定日期发生的日历事件。这不是每周,每月,每年都会发生的情况,但可能会持续13天,14天,15天等等。我有哪些日期会发生,但未能设置重复日期。下面是我的代码在Android的特定日期创建日历事件?

ContentValues calEvent = new ContentValues(); 
      calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); 
      calEvent.put(CalendarContract.Events.TITLE, title); 
      calEvent.put(CalendarContract.Events.DTSTART, d.getTime()); 
      calEvent.put(CalendarContract.Events.DTEND, d.getTime() 
        + (2 * 60 * 60 * 1000)); 
      calEvent.put(CalendarContract.Events.HAS_ALARM, 1); 
      calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone 
        .getDefault().getID()); 
      calEvent.put(CalendarContract.Events.EVENT_LOCATION, location); 
      calEvent.put(CalendarContract.Events.DESCRIPTION, description); 
      calEvent.put(CalendarContract.Events.RDATE, sdf1.format(d) 
        + "T033000Z"); 
      // calEvent.put(CalendarContract.Reminders.RRULE, value); 
      Uri uri = contentResolver.insert(
        CalendarContract.Events.CONTENT_URI, calEvent); 

      // The returned Uri contains the content-retriever URI for the 
      // newly-inserted event, including its id 
      int id = Integer.parseInt(uri.getLastPathSegment()); 
      // Toast.makeText(ctx, "Created Calendar Event " + id, 
      // Toast.LENGTH_SHORT).show(); 
      eventId = eventId + (eventId.equalsIgnoreCase("") ? id : "," + id); 
      ContentValues calReminder = new ContentValues(); 

      calReminder.put(CalendarContract.Reminders.MINUTES, minutes); 
      calReminder.put(CalendarContract.Reminders.EVENT_ID, id); 
      // calReminder.put(CalendarContract.Reminders.HAS_ALARM, 1); 
      calReminder.put(Reminders.METHOD, Reminders.METHOD_ALERT); 
      Uri uri1 = contentResolver.insert(
        CalendarContract.Reminders.CONTENT_URI, calReminder); 

      // The returned Uri contains the content-retriever URI for the 
      // newly-inserted event, including its id 
      int id1 = Integer.parseInt(uri1.getLastPathSegment()); 
      // Toast.makeText(ctx, "Created Calendar Reminder " + id1, 
      // Toast.LENGTH_SHORT).show(); 
      reminderId = reminderId 
        + (reminderId.equalsIgnoreCase("") ? id1 : "," + id1); 

当我使用RDATE与日期的逗号分隔值有STARTDATE提供&结束日期,事件被创建STARTDATE &结束日期之间的所有日期。

+2

嗯......我看到一串代码,一个需求声明,“并没有设置重复日期“ - 实质上”它不起作用“。请将您的问题重新修改为1)确切地说明它如何不起作用(它做什么,不做什么,对特定值有什么期望),以及2)实际上*提出问题*。另请参阅帮助中心中的[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。谢谢。 –

回答

1
Intent service_intent = new Intent(your application context, 
          your class.class); 

        PendingIntent pintent = PendingIntent.getService(
          your application context, 0, service_intent, 0); 

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
        // for 30 mint 60*60*1000 
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, 
          cal.getTimeInMillis(), APP_DAY_FOR_SERVICE 
            * APP_HOUR_FOR_SERVICE 
            * APP_MINUT_FOR_SERVICE 
            * APP_SECOND_FOR_SERVICE * 1000, 
          pintent); 
以24小时的事件此代码后

被称为所以,如果你改变APP_DAY_FOR_SERVICE至2,然后48小时后,事件是老虎,所以如果你想之后的特定日子就像你从压延选择日期,然后调用事件那一天,当天并把答案分钟在APP_DAY_FOR_SERVICE完蛋了

APP_DAY_FOR_SERVICE APP_HOUR_FOR_SERVICE APP_MINUT_FOR_SERVICE这些都是整数,如果你有那么任何查询随意问

+0

但是这不会将事件添加到默认日历。我希望将该活动添加到日历中。使用Alertmanager对我来说并不完美。 – TNR