3

I need to create multiple calendar event for Android application, Using this question I was able to create single event.

Is there any example or guide for create multiple calender events?

Thank You, Chandana

Community
  • 1
  • 1
Chandana
  • 2,578
  • 8
  • 38
  • 55

5 Answers5

6

place these in a function

like

public void calenderevent(Calendar begintime, Calendar endtime){

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", begintime.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime", endtime.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    startActivity(intent);
}
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
  • 3
    Be careful with this. This is an undocumented intent action and could break at anytime or with any future Android releases. It could also break on devices that ship with a propriety Calendar application instead of the stock Android one. There is no official way to create your own events on the Calendar. Although generally this will work... But you've been warned ;) – Brayden Jun 01 '12 at 20:39
3

As of ICS there is a better answer, as documented in the blog there is now an official API.

Blog entry on the calendar APIs in ICS

Here's the documentation on developer.android.com

Cheers!

3

This is all about above Android Build API 8 to ICS 15.

String[] calendarsProjection = {
        CalendarContract.Calendars._ID,
        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
        CalendarContract.Calendars.ACCOUNT_NAME
    };

String calName; 
String calId = null; 
Uri calendars= Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
if (managedCursor.moveToFirst()) 
{

    int nameColumn = managedCursor.getColumnIndex("account_name"); 
    int idColumn = managedCursor.getColumnIndex("_id");
    do 
    {
        calName = managedCursor.getString(nameColumn);
        calId = managedCursor.getString(idColumn);
        Log.e("Calendar Id : ",""+calId+" : "+calName);

    }
    while (managedCursor.moveToNext());
}


long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 18, 13, 10, 10);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 18, 16, 10, 10);
endMillis = endTime.getTimeInMillis();
System.out.println("Date start :"+startMillis);
System.out.println("Date start :"+endMillis);

// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1 );
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);
Pang
  • 9,564
  • 146
  • 81
  • 122
Ashok Domadiya
  • 1,124
  • 13
  • 31
1
String[] calendarsProjection = {
           CalendarContract.Calendars._ID,
           CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
           CalendarContract.Calendars.ACCOUNT_NAME
       };

      String calName; 
            String calId = null; 
      Uri calendars= Uri.parse("content://com.android.calendar/events");
            Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
            if (managedCursor.moveToFirst()) 
            {

                int nameColumn = managedCursor.getColumnIndex("account_name"); 
                int idColumn = managedCursor.getColumnIndex("_id");
                do 
                {
                    calName = managedCursor.getString(nameColumn);
                    calId = managedCursor.getString(idColumn);
                    Log.e("Calendar Id : ",""+calId+" : "+calName);

                }
                while (managedCursor.moveToNext());
            }


      long startMillis = 0;
      long endMillis = 0;
      Calendar beginTime = Calendar.getInstance();
      beginTime.set(2012, 6, 18, 13, 10, 10);
      startMillis = beginTime.getTimeInMillis();
      Calendar endTime = Calendar.getInstance();
      endTime.set(2012, 6, 18, 16, 10, 10);
      endMillis = endTime.getTimeInMillis();
      System.out.println("Date start :"+startMillis);
      System.out.println("Date start :"+endMillis);

      // Insert Event
      ContentResolver cr = getContentResolver();
      ContentValues values = new ContentValues();
      values.put(CalendarContract.Events.DTSTART, startMillis);
      values.put(CalendarContract.Events.DTEND, endMillis);
      values.put(CalendarContract.Events.TITLE, "Walk The Dog");
      values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
      values.put(CalendarContract.Events.CALENDAR_ID, 1 );
      values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
      Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);
iAndroid
  • 951
  • 7
  • 18
0

Well..the last 2 post works fine in ICS but not in others. I suggest this class from google code.

j0k
  • 22,600
  • 28
  • 79
  • 90