As you know we have content providers for addition, deletion of Contacts, Image,Video and Phone Call Log etc.
Similarly we have Content Providers for Calendar Events..
This tutorial helps you in adding and deleting events in android 2.2 calendar. You need to follow the given steps :
1.) Getting required Calendar Name and Id :
A device may have more than one Calendar configure in it. So first, you must find
out available calendar's name and their id.
Using java Syntax Highlighting
- Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"calendar_id", "displayname"}, null, null, null);
- cursor.moveToFirst();
- // fetching calendars name
- String CNames[] = new String[cursor.getCount()];
- // fetching calendars id
- CId = new int[cursor.getCount()];
- for (int i = 0; i < CNames.length; i++)
- {
- CId[i] = cursor.getInt(0);
- CNames[i] = cursor.getString(1);
- cursor.moveToNext();
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
2.) Adding an Event :
Using java Syntax Highlighting
- ContentValues event = new ContentValues();
- // Calendar in which you want to add Evnet
- event.put("calendar_id", CalIds[0]);
- // Title of the Event
- event.put("title", "Birthday");
- // Description of the Event
- event.put("description", "Birthday Party");
- // Venue of the Event
- event.put("eventLocation", "Delhi");
- // Start Date of the Event
- event.put("dtstart", StartDate);
- // End Date of the Event
- event.put("dtend", EndDate);
- // Event is all day
- event.put("allDay", 1);
- // Set alarm on this Event
- event.put("hasAlarm",1);
- Uri eventsUri = Uri.parse("content://com.android.calendar/events");
- // event is added
- getContentResolver().insert(eventsUri, event);
- cursor.close();
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
3.) Deleting an Event :
Using java Syntax Highlighting
- //getContentResolver().delete(path to the content, want to delete, CONDITION, ARGUMENTS);
- //CONDITION + ARGUMENTS work as where condition to find a particular event.
- getContentResolver().delete(Uri.parse("content://com.android.calendar/events"), "calendar_id=? and description=? and eventLocation=? ", new String[]{String.valueOf(CalIds[0]), "Birthday Party", "Delhi"});
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
If you want to do the same in 2.1, you need to do minor modification in above mentioned steps...
1.) Remains Same
2.) change path from "content://com.android.calendar/calendars" to "content://calendar/calendars"
3.) It should be like this :
Using java Syntax Highlighting
- Uri CALENDAR_URI = Uri.parse("content://calendar/events");
- Cursor c = getContentResolver().query(CALENDAR_URI, null, null, null, null);
- if (c.moveToFirst())
- {
- while (c.moveToNext())
- {
- String desc = c.getString(c.getColumnIndex("description"));
- String location = c.getString(c.getColumnIndex("eventLocation"));
- // event id
- String id = c.getString(c.getColumnIndex("_id"));
- if ((desc==null) && (location == null))
- {
- }
- else
- {
- if (desc.equals("Birthday Party") && location.equals("Delhi"))
- {
- Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(id));
- getContentResolver().delete(uri, null, null);
- }
- }
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Hope this will help to understand calendar part easily.
Thanks
Kanika


