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 add event in a default calendar .. please suggest me the changes needed in my code to add event in the calendar of my app
private void addevent() {
// TODO Auto-generated method stub
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(Events.TITLE, "My House Party");
calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House");
calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach");
GregorianCalendar calDate = new GregorianCalendar(2013, 4, 16);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calDate.getTimeInMillis());
startActivity(calIntent);
Toast.makeText(this, "event added", Toast.LENGTH_SHORT).show();
}
if add event in calender use below code , that work in my calender.
// this code for add event in calender.
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
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);
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
Uri uri = cr.insert(EVENTS_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
// this function call upper code.
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = 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.
public class Events {
public static String ALL_DAY = "allDay";
public static String DTSTART = "dtstart";
public static String DTEND = "dtend";
public static String TITLE = "title";
public static String DESCRIPTION = "description";
public static String CALENDAR_ID = "calendar_id";
public static String EVENT_TIMEZONE = "eventTimezone";
public static String VISIBILITY = "visibility";
public static String HASALARM = "hasAlarm";
public static int VISIBILITY_VALUE = 0;
public static int HASALARM_VALUE = 1;
public static int ALL_DAY_VALUE = 0;
}
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,new String[]
{ Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME }, null ,null, null);
else
cursor = getContentResolver().query(Uri.parse("content://com.android.calendar
/calendars"),new String[] { "_id", "displayName" }, "selected=1",null, null);
if (cursor != 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);
System.out.println(""+cursor.getInt(0) + " -- "+
cursor.getString(1));
cursor.moveToNext();
}
cursor.close();