Adding events to Android Device Calendar

Here is the code to add an event to the Calendar
Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null);
            cursor.moveToFirst();

            // Get calendars name
            String calendarNames[] = new String[cursor.getCount()];

            // Get calendars id
            int[] calendarId = new int[cursor.getCount()];
            for (int i = 0; i < calendarNames.length; i++)
            {
                     calendarId[i] = cursor.getInt(0);
                     calendarNames[i] = cursor.getString(1);
                     cursor.moveToNext();
            }
            ContentValues contentEvent = new ContentValues();

            // Particular Calendar in which we need to add Event
            contentEvent.put("calendar_id", calendarId[0]);                                                
            // Title/Caption of the Event   
            contentEvent.put("title", "New event");                                                         
            // Description of the Event
            contentEvent.put("description", "Event Description"); 
          
            // Venue/Location of the Event
            contentEvent.put("eventLocation","Event Location");                     
                           
            // Start Date of the Event with Time
            Calendar cal1 = Calendar.getInstance();
           
            cal1.set(2011,9,5);   // date format (year,month,day of month)
            contentEvent.put("dtstart", cal1.getTimeInMillis());
                                                       
            // End Date of the Event with Time
           Calendar cal2 = Calendar.getInstance();
             cal2.set(2011,9,7);   // date format (year,month,day of month)
          
            contentEvent.put("dtend", cal2.getTimeInMillis()); 
              
            // All Day Event                                     
            contentEvent.put("allDay", 1);   
               
            // Set alarm for this Event                                            
            contentEvent.put("hasAlarm",1);                                                                
            Uri eventsUri = Uri.parse("content://com.android.calendar/events");  

            // event is added successfully
            getContentResolver().insert(eventsUri, contentEvent);
            cursor.close();







Comments

Popular posts from this blog

All about Context

How to use LocalBroadcastManager?

Units used for Measurement in Android