To add the event to device calendar using intent:
This intent will open the calnder add/edit the event screen:Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", stdate);
intent.putExtra("allDay", false);
intent.putExtra("endTime", enddate);
intent.putExtra("title", title_name);
intent.putExtra("description", "");
intent.putExtra("eventLocation", location);
startActivity(intent);
Below provided Event add/remove and Reminder add/remove Uri's work with api level 8 and above .
if your are developing application with below api level 8 you need to change the content Uri to
if(Build.VERSION.SDK_INT >= 8){
Uri.parse("content://com.android.calendar/events") }else {
Uri.parse(
"content://calendar/
events") }
To work with calender provider you need to mention the calendar permission in manifest.xml
<uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" />
To add the event to device calendar in background:
This will add the event with help of calendar provider.
If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings.
Uri eventUriString = Uri.parse("content://com.android.calendar/events");
ContentValues eventValues = new ContentValues();
eventValues.put("calendar_id", 1); // id, We need to choose from our mobile for primary its 1
eventValues.put("title", title);
eventValues.put("description", info);
eventValues.put("eventLocation", place);
eventValues.put("dtstart", startDate);
eventValues.put("dtend", endDate);
eventValues.put("eventTimezone", TimeZone.getDefault().getID());
eventValues.put("eventStatus", 1); // This information is sufficient for most entries tentative (0), confirmed (1) or canceled (2):
eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventUri = cr.insert(eventUriString, eventValues);
//to get the last inserted event id
long eventID = Long.parseLong(eventUri.getLastPathSegment());
To delete the event from calendar id:
If you are developing the application using the api level 14 or above
you can directly use the CalendarContract class and it's constants
instead of below used strings.
Uri eventUri = Uri.parse("content://com.android.calendar/events"); // or
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(eventUri, id);
int rows = cr.delete(deleteUri, null, null);
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(eventUri, id);
int rows = cr.delete(deleteUri, null, null);
To add the reminder for particular Event:
If you are developing the application using the api level 14 or above
you can directly use the CalendarContract class and it's constants
instead of below used strings.
ContentValues reminders = new ContentValues();
reminders.put("event_id", event_id);
reminders.put("method", 1);//Reminder.METHOD_ALERT constant value is 1
reminders.put("minutes", mReminders.get(pos));//Passing this as -1 will use the default reminder minutes.
Uri uri2 = Uri.parse("content://com.android.calendar/reminders");
getContentResolver().insert(uri2, reminders);
ContentValues reminders = new ContentValues();
reminders.put("event_id", event_id);
reminders.put("method", 1);//Reminder.METHOD_ALERT constant value is 1
reminders.put("minutes", mReminders.get(pos));//Passing this as -1 will use the default reminder minutes.
Uri uri2 = Uri.parse("content://com.android.calendar/reminders");
getContentResolver().insert(uri2, reminders);
To get the particular Event reminders:
If you are developing the application using the api level 14 or above
you can directly use the CalendarContract class and it's constants
instead of below used strings.
Uri uri = Uri.parse("content://com.android.calendar/reminders");
Cursor cursor;
cursor=getContentResolver().query(uri, null,"event_id = ?", new String[]{cal_Id}, null);
if (cursor!=null && cursor.moveToFirst()) {
do {
reminder_BO=new Reminder_BO();
reminder_BO.set_id(cursor.getString(cursor.getColumnIndex("_id")));
reminder_BO.setEvent_id(cursor.getString(cursor.getColumnIndex("event_id")));
reminder_BO.setMinutes(cursor.getString(cursor.getColumnIndex("minutes")));
reminder_BO.setMethod(cursor.getString(cursor.getColumnIndex("method")));
reminders.add(reminder_BO);
} while (cursor.moveToNext());
}
cursor.close();
Cursor cursor;
cursor=getContentResolver().query(uri, null,"event_id = ?", new String[]{cal_Id}, null);
if (cursor!=null && cursor.moveToFirst()) {
do {
reminder_BO=new Reminder_BO();
reminder_BO.set_id(cursor.getString(cursor.getColumnIndex("_id")));
reminder_BO.setEvent_id(cursor.getString(cursor.getColumnIndex("event_id")));
reminder_BO.setMinutes(cursor.getString(cursor.getColumnIndex("minutes")));
reminder_BO.setMethod(cursor.getString(cursor.getColumnIndex("method")));
reminders.add(reminder_BO);
} while (cursor.moveToNext());
}
cursor.close();
To remove the particular event reminder :
If you are developing the application using the api level 14 or above you can directly use the CalendarContract class and it's constants instead of below used strings.
Uri uri = Uri.parse("content://com.android.calendar/reminders");
int rows= getContentResolver().delete(uri, "event_id = ? and _id = ?", new String[]{mEventReminders.get(position).get(pos).getEvent_id(),mEventReminders.get(position).get(pos).get_id()});
Very nice. One question: how to add a reminder to a given calendar event when event is just about to be created via intent (as in describer above: To add the event to device calendar in background) and we dont have an event id yet?
ReplyDeleteint eintrag = Integer.parseInt(uri.getLastPathSegment());
ReplyDeleteContentValues reminders = new ContentValues();
reminders.put(CalendarContract.Reminders.EVENT_ID,eintrag);
reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
reminders.put(CalendarContract.Reminders.MINUTES, 0);
Uri uri2 = cr.insert(CalendarContract.Reminders.CONTENT_URI, reminders);
MfG Tino Bellmann