I want to retrive the data of last added event from android calendar. I am using this code to get 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 simply add an 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 this I simply retrieve the data of 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't get the data of this event. I am not getting where is the problem. Please, help me to solve this.
use this code. its working in my aap
public long GetMaxID(ContentResolver cr, Uri cal_uri, Context context)
{
Uri local_uri = cal_uri;
if (cal_uri == null)
{
// local_uri = Uri.parse("content://calendar/calendars/" +
// "events");
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;
}
public static CalendarData GetEventDetails(String ID, Context context)
{
CalendarData temp = null;
ContentResolver contentResolver = context.getContentResolver();
// Fetch a list of all calendars synced with the device, their display
// names and whether the
cursor = contentResolver.query(
Uri.parse("content://com.android.calendar/calendars"),
(new String[]
{ "_id", "displayName", "selected" }), null, null, null);
HashSet<String> calendarIds = new HashSet<String>();
try
{
System.out.println("Count=" + cursor.getCount());
if (cursor.getCount() > 0)
{
System.out
.println("the control is just inside of the cursor.count loop");
while (cursor.moveToNext())
{
String _id = cursor.getString(0);
String displayName = cursor.getString(1);
Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Id: " + _id + " Display Name: "
+ displayName + " Selected: " + selected);
calendarIds.add(_id);
}
}
}
catch (AssertionError ex)
{
ex.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
// For each calendar, display all the events from the previous week to
// the end of next week.
// for (String id : calendarIds)
for (int id = 0; id <= calendarIds.size(); id++)
{
Uri.Builder builder = Uri.parse(
"content://com.android.calendar/instances/when")
.buildUpon();
// Uri.Builder builder =
// Uri.parse("content://com.android.calendar/calendars").buildUpon();
long now = new Date().getTime();
ContentUris
.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
ContentUris
.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[]
{ "_id", "title", "begin", "end", "allDay" },
"Calendars._id=" + id, null, null);
System.out.println(id + " eventCursor count="
+ eventCursor.getCount());
if (eventCursor.getCount() > 0)
{
eventCursor.moveToFirst();
while (eventCursor.moveToNext())
{
Object mbeg_date, beg_date, beg_time, end_date, end_time;
final String eventID = eventCursor.getString(0);
final String title = eventCursor.getString(1);
final Date begin = new Date(eventCursor.getLong(2));
final Date end = new Date(eventCursor.getLong(3));
final Boolean allDay = !eventCursor.getString(4)
.equals("0");
if (eventID.equals(ID))
{
temp = new CalendarData();
temp.Title = eventCursor.getString(1);
temp.StartDate = eventCursor.getLong(2);
temp.EndDate = eventCursor.getLong(3);
break;
}
}
}
// break;
}
return temp;
}
I used the following code to retrieve all the data on the Calendar which is:
cr = getApplicationContext().getContentResolver();
caluri=CalendarContract.Events.CONTENT_URI;
atteuri=CalendarContract.Attendees.CONTENT_URI;
try
{
cur1 = cr.query(caluri, new String[]{Events.CALENDAR_ID,Events._ID, Events.TITLE, Events.DESCRIPTION,Events.DTSTART, Events.DTEND, Events.EVENT_LOCATION }, null, null, null);
if(cur1!=null){
while(cur1.moveToNext()){
cal_ID=cur1.getString(cur1.getColumnIndex(Events.CALENDAR_ID));
event_ID=cur1.getString(cur1.getColumnIndex(Events._ID));
cur2=cr.query(atteuri,new String[]{Attendees.ATTENDEE_NAME,Attendees.ATTENDEE_EMAIL}, Attendees.EVENT_ID +"=" +event_ID, null, null);
if(cur2!=null){
while(cur2.moveToNext()){
event_Title=cur1.getString(cur1.getColumnIndex(Events.TITLE));
event_Desc=cur1.getString(cur1.getColumnIndexOrThrow(Events.DESCRIPTION));
event_Start=new Date(cur1.getLong(cur1.getColumnIndex(Events.DTSTART)));
event_end=new Date(cur1.getLong(cur1.getColumnIndex(Events.DTEND)));
event_loc=cur1.getString(cur1.getColumnIndex(Events.EVENT_LOCATION));
attendee_name=cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_NAME));
attendee_Email=cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_EMAIL));
all_attendee +="\n"+attendee_name;
all_Emails +="\n"+attendee_Email;
}
cur2.close();
}
all +="Event title: " + event_Title + "\n" + "Event Description: " + event_Desc + "\n" +"Event Start: " + event_Start + "\n" + "Events End: " + event_end + "\n" + "Event Location: " + event_loc + "\n" + "Attendees: " + "\n" + all_attendee + "\n" + "Emails: "+ "\n" + all_Emails + "\n";
}
cur1.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
SO all what you need as I guess is to adjust it little bit get data for last event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With