2016-04-27 113 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

相關問題