Android Calendar Events
I am just making an operation with Calendar Content Provider, now I am failing at the point to display events for the particular date.
I know the Events URI for the < 2.1 version and 2.2 version, as below:
eventsUri = Uri.parse("content://calendar/events"); // < Android 2.1 version
eventsUri = Uri.parse("content://com.android.calendar/events"); // For Android Froyo 2.2 and later version
My doubts are:
- How do I Fetch all the events?
- How do I fetch events for the particular date?
So please, somebody with knowledge about the Calendar please help me and share your knowledge regarding this.
Thanx
These examples are for <= 2.1 version;
first; find out which calendars exist
Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
Fetching all events, and particular event is done by specifying rangeContentResolver contentResolver = getContentResolver()
;
Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);
and then let's say you wish to log events ID from calendar with ID = 1
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "event_id"}, "Calendars._id=" + 1,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
String uid2 = eventCursor.getString(0);
Log.v("eventID : ", uid2);
}
// Here's a way for you to get all the Calendar events list
public static void readCalendarEvent(Context context) {
ContentResolver contentResolver = context.getContentResolver();
cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), (new String[] { "_id", "title", "organizer", "dtstart", "dtend"}), null, null, null);
List<GoogleCalendar> gCalendar = new ArrayList<GoogleCalendar>();
try {
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
GoogleCalendar googleCalendar = new GoogleCalendar();
int event_ID = cursor.getInt(0);
googleCalendar.setEvent_id(event_ID);
String title = cursor.getString(1);
googleCalendar.setTitle(title);
String mOrganizer = cursor.getString(2);
googleCalendar.setOrganizer(mOrganizer);
String dtStart = cursor.getString(3);
googleCalendar.setDtstart(dtStart);
String dtEnd = cursor.getString(4);
googleCalendar.setDtend(dtEnd);
gCalendar.add(googleCalendar);
Log.d("CaledarM",googleCalendar.getTitle()+" name = "+googleCalendar.getOrganizer()+" dateStart = "+googleCalendar.getDtstart()+" Size = " + gCalendar.size());
}
}
} catch (AssertionError ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// class GoogleCalendar()
public class GoogleCalendar {
private int event_id;
private String title,
organizer,
dtstart,
dtend;
public GoogleCalendar()
{
}
public int getEvent_id() {
return event_id;
}
public void setEvent_id(int calendar_id) {
event_id = calendar_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getOrganizer() {
return organizer;
}
public void setOrganizer(String description) {
this.organizer = description;
}
public String getDtstart() {
return dtstart;
}
public void setDtstart(String dtstart1) {
this.dtstart = dtstart1;
}
public String getDtend() {
return dtend;
}
public void setDtend(String dtend1) {
this.dtend = dtend1;
}
}