7

Adding Events for all days between start date and end date in google calendar Android. i want remainder every 3 months till end date. This is my function

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

this function adds events every day. How to remove that. I need only remainder.Is there any anything wrong in my code?? screenshot1screenshot2

Elizabeth
  • 1,399
  • 1
  • 13
  • 25
  • Check this [SO question](http://stackoverflow.com/questions/6298235/get-google-calendar-events-start-and-end-times-with-google-java-api-client-in-an?rq=1) if it can help you :) – KENdi Apr 28 '16 at 02:18
  • is it necessary to use api?? no other method?? – Elizabeth May 05 '16 at 08:02
  • @Elizabeth is there any way that I could still help you out, I haven't received any response so far. – jobbert May 20 '16 at 07:26

1 Answers1

4

If I read everything correctly you want a calendar item every tree months for one full day. Have you tried adding this line?

contentValues.put(CalendarContract.EXTRA_EVENT_ALL_DAY, true);

And changed the end date of one calendar item is currrently set to the end of the year while this should be the end date of the current item. DTEND is the end of the current item DURATION is the end of the reoccuring patern.

If I understood the problem wrong please give me a detailed description. For all options in the CalenderContracts check this link.

EDIT:

You want a calendar appointment for each day but remind the user every three months. With your code this is not currently possible as you are adding a reminder for every calender item with that id (so for every day). Only easy solution what I can think of is creating a separate appointment with an other id that you repeat every three months with an reminder and a different id. It is not currently possible to set an alarm for some events with the same id and some not.

jobbert
  • 3,297
  • 27
  • 43
  • let start date be jan 1 2016 and end date jan 1 2017. Now i want notification on april 1 2016, july 1 2016, oct 1 2016 and jan 1 2017 only. but when i open and check my calendar i can see everyday is marked. which i dont want to. Any help??? – Elizabeth May 23 '16 at 05:08
  • @Elizabeth, you only want an appointment for those days too? If so, simply set the end date to the same date of the start date of your first appointment :) – jobbert May 23 '16 at 08:16
  • how?? i didnt get you?? – Elizabeth May 23 '16 at 08:38
  • What you are currently doing is creating a recurring appointment with start date 1 januari 2016 and end date 1 januari 2017. What you should do is create a recurring appointment with start date 1 januari 2016 and end date 1 januari 2016 but the recurring ends at 1 januari 2017. The appointment should also contain CalendarContract.EXTRA_EVENT_ALL_DAY as it is not time specific (in your case). – jobbert May 23 '16 at 09:05
  • getting error : java.lang.IllegalArgumentException: allDay is true but sec, min, hour are not 0. when 'CalendarContract.EXTRA_EVENT_ALL_DAY true ' is added – Elizabeth May 24 '16 at 05:37
  • if i didnt use that then its working...is that required?? – Elizabeth May 24 '16 at 06:33
  • It kinda is. Because I think the appointment is day bound right? (not time bound). Try to set the start dates' time to 00:00:00 and remove the end date (NOT DTEND). To set the start dates' time to 00:00:00 just change your DateFormat to: `SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());` – jobbert May 24 '16 at 07:22
  • no now its not working..can i set time as user current time? whether it will work?? to remove end date i just deleted ' dt.setTime(Enddate);' this line..is it ok? – Elizabeth May 24 '16 at 08:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112764/discussion-between-elizabeth-and-jobbert). – Elizabeth May 24 '16 at 09:35