I want to extract the data of the last added event from the Android calendar . I use this code to get the last id.
public static long getNewEventId(ContentResolver cr, Uri cal_uri) { Uri local_uri = cal_uri; if (cal_uri == null) { local_uri = Uri.parse("content://com.android.calendar/events"); } Cursor cursor = cr.query(local_uri, new String[] { "MAX(_id) as max_id" }, null, null, "_id"); cursor.moveToFirst(); long max_val = cursor.getLong(cursor.getColumnIndex("max_id")); return max_val + 1; }
And then I just add the event using this code:
Intent intent = new Intent(Intent.ACTION_EDIT); intent.setType("vnd.android.cursor.item/event"); intent.putExtra("beginTime", SelectedDate); intent.putExtra("allDay", false); intent.putExtra("rrule", "FsREQ=DAILY"); intent.putExtra("endTime", SelectedDate + 60 * 60 * 1000); intent.putExtra("title", "Advance Scheduler Event"); startActivity(intent);
After that, I simply retrieve the data for this event using this code:
public CalendarData EventDetails(int ID) { CalendarData temp = null; // ------------------------------------------------------------------------------- ContentResolver cr = getContentResolver(); Cursor cursor_calendar; if (Integer.parseInt(Build.VERSION.SDK) >= 8) { cursor_calendar = cr.query( Uri.parse("content://com.android.calendar/calendars"), new String[] { "_id", "displayname" }, null, null, null); } else { cursor_calendar = cr.query( Uri.parse("content://calendar/calendars"), new String[] { "_id", "displayname" }, null, null, null); } cursor_calendar.moveToFirst(); String[] CalNamess = new String[cursor_calendar.getCount()]; int[] CalIdss = new int[cursor_calendar.getCount()]; for (int i = 0; i < CalNamess.length; i++) { CalIdss[i] = cursor_calendar.getInt(0); CalNamess[i] = cursor_calendar.getString(1); cursor_calendar.moveToNext(); } cursor_calendar.close(); // ------------------------------------------------------------------------------- Cursor cursor_event; if (Integer.parseInt(Build.VERSION.SDK) >= 8) { cursor_event = cr.query( Uri.parse("content://com.android.calendar/events"), new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null); } else { cursor_event = cr.query(Uri.parse("content://calendar/events"), new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, null, null, null); } boolean flag = false; String add = null; cursor_event.moveToFirst(); String[] CalNames = new String[cursor_event.getCount()]; int[] CalIds = new int[cursor_event.getCount()]; for (int i = 0; i < CalNames.length; i++) { CalIds[i] = cursor_event.getInt(0); if (ID == CalIds[i]) { flag = true; Toast.makeText(getApplicationContext(), "ID Found : " + CalIds[i], Toast.LENGTH_LONG).show(); CalNames[i] = "Event" + cursor_event.getInt(0) + ": \nTitle: " + cursor_event.getString(1) + "\nDescription: " + cursor_event.getString(2) + "\nStart Date: " + cursor_event.getLong(cursor_event .getColumnIndex("dtstart")) + cursor_event.getLong(cursor_event .getColumnIndex("dtend")) + cursor_event.getString(5); temp = new CalendarData(); temp.Title = cursor_event.getString(1); temp.Description = cursor_event.getString(2); // temp.StartDate = new Date(cursor_event.getLong(3)); // temp.EndDate = new Date(cursor_event.getLong(4)); temp.StartDate = cursor_event.getLong(cursor_event .getColumnIndex("dtstart")); temp.EndDate = cursor_event.getLong(cursor_event .getColumnIndex("dtend")); temp.Location = cursor_event.getString(5); break; } cursor_event.moveToNext(); } return temp; }
But I can not get the data of this event. I do not understand where the problem is. Please help me solve this problem.