2016-04-27 108 views
6

在Google日历Android中为开始日期和结束日期之间的所有日期添加活动。我希望每3个月的剩余时间到结束日期。 这是我的功能在Google日历中为开始日期和结束日期之间的所有日期添加活动Android

public void addEvent1(Context ctx, String title){ 
      SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yyyy"); 
      SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault()); 
      Date Startdate = null; 
      Date Enddate =null; 
      String dtStart = date.getText().toString(); 
      try { 
       Startdate = df2.parse(dtStart); 
       Enddate = df2.parse(stringMaturityDate); 
       Log.v("SDate: ",""+ df3.format(Startdate)); 
       Log.v("EDate: ",""+ df3.format(Enddate)); 
      } catch(ParseException e){ 
       e.printStackTrace(); 
      } 
      Calendar cali = Calendar.getInstance(); 
      cali.setTime(Startdate); 


      Calendar cali2 = Calendar.getInstance(); 
      cali2.setTime(Enddate); 

      SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyyMMdd"); 
      Calendar dt = Calendar.getInstance(); 


      dt.setTime(Enddate); 

      String dtUntill = yyyymmdd.format(dt.getTime()); 

      ContentResolver contentResolver = ctx.getContentResolver(); 

      ContentValues calEvent = new ContentValues(); 
      calEvent.put(CalendarContract.Events.CALENDAR_ID, 1); // XXX pick) 
      calEvent.put(CalendarContract.Events.TITLE, title); 
      calEvent.put(CalendarContract.Events.RRULE, "FREQ=MONTHLY;INTERVAL=3;UNTIL=" + dtUntill); 
      calEvent.put(CalendarContract.Events.DTSTART, cali.getTimeInMillis()); 
      calEvent.put(CalendarContract.Events.DTEND, cali2.getTimeInMillis()); 

      calEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "" + java.util.Locale.getDefault()); 



      Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, calEvent); 


       int id = Integer.parseInt(uri.getLastPathSegment()); 
       Toast.makeText(ctx, "Created Calendar Event " + id, 
         Toast.LENGTH_SHORT).show(); 
ContentValues reminders = new ContentValues(); 
     reminders.put(CalendarContract.Reminders.EVENT_ID, id); 
     reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); 
     reminders.put(CalendarContract.Reminders.MINUTES, 10); 

     Uri uri1 = contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminders); 
     } 

这个函数每天都会添加事件。如何删除。我只需要余数。在我的代码中是否有任何错误? screenshot1screenshot2

+0

检查此问题[SO问题](http://stackoverflow.com/questions/6298235/get-google-calendar-events-start-and-end-times-with-google-java-api-client-in-一个?rq = 1)如果它可以帮助你:) – KENdi

+0

是否有必要使用API​​?没有其他方法? – Elizabeth

+0

@伊丽莎白有什么方法可以帮助你,迄今为止我还没有收到任何回应。 – jobbert

回答

3

如果我正确地阅读了所有内容,则需要每隔一个月的一个日历项目一整天。你有没有尝试添加这一行?

contentValues.put(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 

而且改变一个日历项目的结束日期currrently设置到今年年底,而这应该是当前项目的结束日期。 DTEND是当前项目的结尾DURATION是重复模式的结束。

如果我了解错误的问题,请给我一个详细的描述。对于CalenderContracts中的所有选项,请检查this link

编辑:

你想每一天的日历约会,但提醒用户每三个月。随着你的代码,这是目前不可能的,因为你正在添加提醒每个日历与该id(所以每天)。我能想到的唯一简单的解决方案是创建一个单独的约会与其他身份证,你每三个月重复一次提醒和一个不同的ID。目前不可能为某些具有相同ID的事件设置闹钟,有些则不可以。

+0

让开始日期为1月1日2016年和结束日期1月2017年。现在我想通知2016年4月1日,2016年7月1日,2016年1月1日和1月1日2017年只。但是当我打开并检查我的日历时,我可以看到每天都被标记。我不想要。任何帮助? – Elizabeth

+0

@伊丽莎白,你只需要预约那些日子?如果是这样,只需将结束日期设置为您首次约会的开始日期的相同日期:) – jobbert

+0

如何?我没有让你? – Elizabeth

相关问题