Chandana
Chandana

Reputation: 2648

Android create calendar event

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

Upvotes: 3

Views: 11920

Answers (5)

Ashok Domadiya
Ashok Domadiya

Reputation: 1122

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

Upvotes: 3

futer24
futer24

Reputation: 1

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

Upvotes: 0

iAndroid
iAndroid

Reputation: 951

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

Upvotes: 1

Rich Ralston
Rich Ralston

Reputation: 67

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!

Upvotes: 3

PedroAGSantos
PedroAGSantos

Reputation: 2346

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

Upvotes: 6

Related Questions