Want To Add Event To Calender In Android
HI I am developing a android app in which I am suppose to handle calendar . I am unable to add event in my own calendar . I have used following code . using that code I am able to
Solution 1:
if add event in calender use below code , that work in my calender.
// this code for add event in calender.
ContentResolvercr= getContentResolver();
ContentValuesvalues=newContentValues();
values.put(Events.ALL_DAY, Events.ALL_DAY_VALUE);
values.put(Events.DTSTART, startTimeInMilli);
values.put(Events.DTEND, endTimeInMilli);
values.put(Events.TITLE, strTaskName);
values.put(Events.DESCRIPTION, strDescription);
values.put(Events.CALENDAR_ID, 0);
values.put(Events.VISIBILITY, Events.VISIBILITY_VALUE);
UriEVENTS_URI= Uri.parse(getCalendarUriBase(this) + "events");
Uriuri= cr.insert(EVENTS_URI, values);
longeventID= Long.parseLong(uri.getLastPathSegment());
// this function call upper code.
private String getCalendarUriBase(Activity act) {
StringcalendarUriBase=null;
Uricalendars= Uri.parse("content://calendar/calendars");
CursormanagedCursor=null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null,
null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
// other static class for event constants.
publicclassEvents {
publicstaticStringALL_DAY="allDay";
publicstaticStringDTSTART="dtstart";
publicstaticStringDTEND="dtend";
publicstaticStringTITLE="title";
publicstaticStringDESCRIPTION="description";
publicstaticStringCALENDAR_ID="calendar_id";
publicstaticStringEVENT_TIMEZONE="eventTimezone";
publicstaticStringVISIBILITY="visibility";
publicstaticStringHASALARM="hasAlarm";
publicstaticintVISIBILITY_VALUE=0;
publicstaticintHASALARM_VALUE=1;
publicstaticintALL_DAY_VALUE=0;
}
Solution 2:
You need to pass calendar id of your app in your intent putExtra.
calIntent.putExtra(Events.CALENDAR_ID , my_cal_id);
To get list of all available calendar in your device you can use following code:
if(android.os.Build.VERSION.SDK_INT
>= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
cursor = getContentResolver().query(Calendars.CONTENT_URI,newString[]
{ Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME }, null ,null, null);
else
cursor = getContentResolver().query(Uri.parse("content://com.android.calendar
/calendars"),newString[] { "_id", "displayName" }, "selected=1",null, null);
if (cursor != null && cursor.moveToFirst()) {
String[] calNames = newString[cursor.getCount()];
int[] calIds = newint[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
System.out.println(""+cursor.getInt(0) + " -- "+
cursor.getString(1));
cursor.moveToNext();
}
cursor.close();
Post a Comment for "Want To Add Event To Calender In Android"