I am trying to create a app which will fetch details of Events and Attendeesfrom Calendar app.
I am facing the problems which are"
1). In many of the events Title and their attendees does not match.
2). In many of the events I am getting 0 attendees
(mainly for upcoming events).
Here is my code: (Please let me know the mistake).
public class ReadCalendar {
static Cursor cursor;
public static void readCalendar(Context context) {
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[] { Calendars._ID, Calendars.NAME}), 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);
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) {
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);
Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
null, "_id ASC");
Log.e("123","eventCursor count====="+eventCursor.getCount());
if(eventCursor.getCount()>0)
{
if(eventCursor.moveToFirst())
{
do
{
Object mbeg_date,beg_date,beg_time,end_date,end_time;
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3).equals("0");
final String eventId = eventCursor.getString(4);
final String calendarID = eventCursor.getString(5);
Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);
/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
Log.e("123","Title:"+title);
Log.e("123","Begin:"+begin);
Log.e("123","End:"+end);
Log.e("123","All Day:"+allDay);
// Attendees Code
Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
if(eventAttendeesCoursor.getCount()>0)
{
if(eventAttendeesCoursor.moveToFirst())
{
do {
// Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
} while(eventAttendeesCoursor.moveToNext());
}
}
}
while(eventCursor.moveToNext());
}
}
break;
}
}
}
Related
I am trying to create a app which will fetch details of Events and Attendees from Calendar app on device.
I am facing the problems which are:
1). In many of the events Title and their attendees does not match.
2). In many of the events I am getting 0 attendees (mainly for upcoming events).
Here is my code: (Please let me know the mistake).
public class ReadCalendar {
static Cursor cursor;
public static void readCalendar(Context context) {
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[] { Calendars._ID, Calendars.NAME}), 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);
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) {
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);
Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
null, "_id ASC");
Log.e("123","eventCursor count====="+eventCursor.getCount());
if(eventCursor.getCount()>0)
{
if(eventCursor.moveToFirst())
{
do
{
Object mbeg_date,beg_date,beg_time,end_date,end_time;
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3).equals("0");
final String eventId = eventCursor.getString(4);
final String calendarID = eventCursor.getString(5);
Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);
/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
Log.e("123","Title:"+title);
Log.e("123","Begin:"+begin);
Log.e("123","End:"+end);
Log.e("123","All Day:"+allDay);
// Attendees Code
Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
if(eventAttendeesCoursor.getCount()>0)
{
if(eventAttendeesCoursor.moveToFirst())
{
do {
// Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
} while(eventAttendeesCoursor.moveToNext());
}
}
}
while(eventCursor.moveToNext());
}
}
break;
}
}
}
I didnt look in details into your code, but i just ran into the same issue - and it was because of a mixup between the instance id and the event id. I did see that your uri (builder) is based on instances, but the field you're requiring is event.id : these are distinct.
The important thing is that the attendees table is based on the EVENT id - and not the instance id - so it's likely to explain your issues.
Put it all in order, consistently (i.e. extracting the EVENT id from the INSTANCE table based on your URI and pass it on to the ATTENDEES table. See if that helps.
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.
ContentResolver cr = getContentResolver();
Uri caluri = CalendarContract.Events.CONTENT_URI;
Uri atteuri = CalendarContract.Attendees.CONTENT_URI;
Cursor cur1, cur2;
String all = null;
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())
{
String event_Title = cur1.getString(cur1.getColumnIndex(Events.TITLE));
String event_Desc = cur1.getString(cur1.getColumnIndexOrThrow(Events.DESCRIPTION));
Date event_Start = new Date(cur1.getLong(cur1.getColumnIndex(Events.DTSTART)));
Date event_end = new Date(cur1.getLong(cur1.getColumnIndex(Events.DTEND)));
String event_loc = cur1.getString(cur1.getColumnIndex(Events.EVENT_LOCATION));
String all_attendee = null;
String all_Emails = null;
String cal_ID = cur1.getString(cur1.getColumnIndex(Events.CALENDAR_ID));
String 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())
{
String attendee_name = cur2.getString(cur2.getColumnIndex(Attendees.ATTENDEE_NAME));
String 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();
}
System.out.println("My log--------" + all);
I m tring to display the calendar events in android version 4 which can give the arraylist for the title and other details for the events like this but unable to display.
I get the error as calendar not found.
public void readCalendar1()
{
ContentResolver contentResolver = this.getContentResolver();
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
Cursor cursor;
cursor = contentResolver.query(CalendarContract.Calendars.CONTENT_URI, new String[]
{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME },
null, null, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + " ASC");
HashSet<String> calendarIds1 = new HashSet<String>();
while (cursor.moveToNext())
{
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
System.out.println("Id: " + _id + " Display Name: " + displayName );
calendarIds1.add(_id);
}
// For each calendar, display all the events from the previous week to the end of next week.
for (String id : calendarIds1)
{
Uri builder = CalendarContract.Events.CONTENT_URI;
System.out.println("str in read cal1 "+strconvert+"str2 in read cal1 "+strconvert1);
//strconvert and strconvert1 are string which have the particular dates
Cursor eventCursor = contentResolver.query(builder,
new String[] { CalendarContract.Events.TITLE, strconvert,
strconvert1, CalendarContract.Events.ALL_DAY,CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.HAS_ALARM,CalendarContract.Events.DESCRIPTION},
"calendar_id=" + id,null,"dtstart ASC" );
// For a full list of available columns see http://tinyurl.com/yfbg76w
int n=eventCursor.getCount();
System.out.println("No. of rows is="+n);
while(eventCursor.moveToNext())
{
title = eventCursor.getString(0);
begin = new Date(eventCursor.getLong(1));
end = new Date(eventCursor.getLong(2));
allDay = !eventCursor.getString(3).equals("0");
loc=eventCursor.getString(4);
hasalarm = !eventCursor.getString(5).equals("0");
desc=eventCursor.getString(6);
titlestr.add(title);
sdatestr.add(begin.toString());
edatestr.add(end.toString());
locstr.add(loc);
descstr.add(desc);
alarmstr.add(hasalarm.toString());
System.out.println("Title String: " + titlestr);
System.out.println("Begin String: " + sdatestr);
System.out.println("End String: " + edatestr);
System.out.println("Loc String: " + locstr);
System.out.println("Desc String: " + descstr);
System.out.println("Alarm String: " + alarmstr);
// }
System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay+" Location="+loc+" Descriptn="+desc);
}
}
}
This code is working for me to read for both versions below ics android 4 and above ics
//for os version android bELOW version 4(ICS)
public static boolean eventChecker(Context context,ContentResolver cr,String calID){
Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now);
ContentUris.appendId(builder, now + DateUtils.YEAR_IN_MILLIS);
Cursor eventCursorr = cr.query(builder.build(),
new String[] { "title", "begin","description"}, "Calendars._id=" + calID,
null, "startDay ASC, startMinute ASC");
while (eventCursorr.moveToNext()) {
final String titler = eventCursorr.getString(0).trim();
final Date beginr = new Date(eventCursorr.getLong(1));
final String descriptionr = eventCursorr.getString(2).trim();
SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stimesr = sdfrr.format(beginr);
System.out.println("titler "+titler +"stimesr "+stimesr +"descriptionr "+descriptionr );
}
}
//for os version android version 4(ICS) AND ABOVE
#TargetApi(14)
public static boolean eventChecker14(Context context,ContentResolver contentResolver,String calID){
Uri builder = CalendarContract.Events.CONTENT_URI;
String[] COLS = new String[]{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART,CalendarContract.Events.DESCRIPTION};
Cursor eventCursor = contentResolver.query(builder,
COLS,
"calendar_id=" + calID,null,"dtstart ASC" );
int n=eventCursor.getCount();
System.out.println("No. of rows is="+n);
while(eventCursor.moveToNext())
{
String title1 = eventCursor.getString(0).trim();
Date begin1 = new Date(eventCursor.getLong(1));
String desc1=eventCursor.getString(2).trim();
SimpleDateFormat sdfrr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String stimesr = sdfrr.format(begin1);
System.out.println("title1"+title1+"desc1"+desc1+"stimesr"+stimesr);
}
}
I am using below code to get calendar events in my application. It's working fine in Android phones but when I try this code on Android tablet, my application crashes. So I don't know exactly what is the problem and why it is not working on tablets.
public void syncCalander() {
try {
nameValues = new ArrayList<NameValuePair>();
StringBuffer calbuffers;
int cnt = 1;
StringBuffer calbufferimeis = new StringBuffer();
ContentResolver contentResolver = getApplicationContext()
.getContentResolver();
final Cursor cursor = contentResolver.query(
Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null,
null, null);
if (cursor.getCount() == 0) {
} else {
HashSet<String> calendarIds = new HashSet<String>();
CalendarModel calModel = new CalendarModel();
CalendarModel.CALENDERLIST.add(calModel);
int val = cursor.getCount();
Log.i("=============total event============>", "." + val);
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
calModel.setCalendarEvent(displayName);
CalendarModel.CALENDERLIST.add(calModel);
Log.i("--------Display Name----------", "" + "Id: " + _id
+ " Display Name: " + displayName + " Selected: "
+ selected);
calendarIds.add(_id);
Log.i("============celenderIDs==========>", "."
+ calendarIds);
}
for (String id : calendarIds) {
Uri.Builder builder = Uri.parse(
"content://com.android.calendar/instances/when")
.buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now
- DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(builder, now
+ DateUtils.WEEK_IN_MILLIS);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay" },
"Calendars._id=" + id, null,
"startDay ASC, startMinute ASC");
Log.i("============cursor size===========>", "."
+ eventCursor.getCount());
while (eventCursor.moveToNext()) {
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3)
.equals("0");
calModel.setCalendarDate(begin.toString());
CalendarModel.CALENDERLIST.add(calModel);
Log.i("-----Title--------", "Title: " + title
+ " Begin: " + begin + " End: " + end
+ " All Day: " + allDay);
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String date = formatter.format(begin);
int callength = CalendarModel.CALENDERLIST.size();
calbuffers = new StringBuffer();
calbuffers.append("{\"Calenderevent\":\"" + title
+ "\"," + "\"Calenderdate\":\"" + date + "\"}");
calbuffers.append(",");
calbufferimeis.append(calbuffers);
}
eventCursor.close();
}
}
cursor.close();
} catch (Exception e) {
}
}
i have use
content://calendar/calendars
instead of
content://com.android.calendar/calendars
for the android tablet applications
I want to get calendar event updates (when a new event is added or an existing event is deleted ) on android 2.2 devices ?
i am beginner so tell me step by step solution for this..
This code might help you :)
public class myCalendar
{
static Cursor cursor;
public static void readCalendar(Context context) {
ContentResolver contentResolver = context.getContentResolver();
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
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) {
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[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + 1,
null, "startDay ASC, startMinute ASC");
System.out.println("eventCursor count="+eventCursor.getCount());
if(eventCursor.getCount()>0)
{
eventCursor.moveToFirst();
while (eventCursor.moveToNext())
{
Object beg_date,beg_time,end_date,end_time;
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3).equals("0");
/* System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
*/
System.out.println("Title:"+title);
System.out.println("Begin:"+begin);
System.out.println("End:"+end);
System.out.println("All Day:"+allDay);
System.out.println("only date begin of events="+begin.getDate());
System.out.println("only begin time of events="+begin.getHours() + ":" +begin.getMinutes() + ":" +begin.getSeconds());
System.out.println("only date begin of events="+end.getDate());
System.out.println("only begin time of events="+end.getHours() + ":" +end.getMinutes() + ":" +end.getSeconds());
beg_date = begin.getDate();
beg_time = begin.getHours()+":"+begin.getMinutes();
end_date = end.getDate();
end_time = end.getHours()+":"+end.getMinutes();
}
}
break;
}
}
}