Adding event with reminders to calendar with 'Intent.putExtra()' way of doing
Solution 1
No answer according to the method I pointed out, but another way I found to add event with reminders to the calendar.
Hereunder methods are working fine for me :
// Add an event to the calendar of the user.
public void addEvent(Context context) {
GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
values.put(CalendarContract.Events.TITLE, this._title);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
.getTimeZone().getID());
System.out.println(Calendar.getInstance().getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
this._eventId = Long.parseLong(uri.getLastPathSegment());
// Add a 5 minute, 1 hour and 1 day reminders (3 reminders)
setReminder(cr, this._eventId, 5);
setReminder(cr, this._eventId, 60);
setReminder(cr, this._eventId, 1440);
} catch (Exception e) {
e.printStackTrace();
}
}
// routine to add reminders with the event
public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
try {
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, timeBefore);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
Cursor c = CalendarContract.Reminders.query(cr, eventID,
new String[]{CalendarContract.Reminders.MINUTES});
if (c.moveToFirst()) {
System.out.println("calendar"
+ c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// function to remove an event from the calendar using the eventId stored within the Task object.
public void removeEvent(Context context) {
ContentResolver cr = context.getContentResolver();
int iNumRowsDeleted = 0;
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
iNumRowsDeleted = cr.delete(eventUri, null, null);
Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");
}
public int updateEvent(Context context) {
int iNumRowsUpdated = 0;
GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, this._title);
event.put("hasAlarm", 1); // 0 for false, 1 for true
event.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
event.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
iNumRowsUpdated = context.getContentResolver().update(eventUri, event, null,
null);
// TODO put text into strings.xml
Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");
return iNumRowsUpdated;
}
Hope this could help others that encountered the same problem I had :).
Alex.
Solution 2
Agree with above answer but important is calender Id. you can not use 1 as Samsung phone uses 1 for their calendar(S Planner). So calendar ID is the id for which email you want the event. You can get calendar id by following code for a specific event
int calenderId=-1;
String calenderEmaillAddress="[email protected]";
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME};
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
CalendarContract.Calendars.NAME + "=? or " +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
new String[]{calenderEmaillAddress, calenderEmaillAddress,
calenderEmaillAddress}, null);
if (cursor.moveToFirst()) {
if (cursor.getString(1).equals(calenderEmaillAddress))
calenderId=cursor.getInt(0); //youre calender id to be insered in above answer
}
![Alexandre](https://lh5.googleusercontent.com/-PBR4pVxzr-M/AAAAAAAAAAI/AAAAAAAAAAA/Q0b0Bs4ldkw/photo.jpg?sz=256)
Alexandre
Updated on June 14, 2022Comments
-
Alexandre about 2 years
I'm trying to add events to calendar with the following code :
public Intent calPopulation() { Intent calIntent = new Intent(Intent.ACTION_INSERT); calIntent.setType("vnd.android.cursor.item/event"); calIntent.putExtra(CalendarContract.Events.TITLE, this._title); GregorianCalendar calDate = new GregorianCalendar(this._year,this._month, this._day, this._hour, this._minute); calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate.getTimeInMillis()); calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate.getTimeInMillis()+60*60*1000); calIntent.putExtra(CalendarContract.Events.HAS_ALARM, true); calIntent.putExtra(CalendarContract.Reminders.EVENT_ID, CalendarContract.Events._ID); calIntent.putExtra(CalendarContract.Events.ALLOWED_REMINDERS, "METHOD_DEFAULT"); calIntent.putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); calIntent.putExtra(CalendarContract.Reminders.MINUTES,5); return calIntent; }
And then launch the action with :
startActivity(mTask.calPopulation());
I don't have any issue, the calendar app event launched with the correct information I entered into my app, except that it does not fill in the event the reminder I would like to add.
Do you have any clues? I tried to search within many threads using this method (I mean the intent.putExtra) but never find anything interesting.
Other point, is there a way to directly save the event + reminder into the calendar without opening calendar App and requesting for user action?
Thanks in advance. Alex.
-
Singed over 8 yearsThis method adds event directly to the calendar, therefore requires calendar reading/writing permissions. For us who prefer using intent for sending event information to calendar app, is there any way to prepopulate the "reminder" data on event creation screen?
-
Nooruddin Lakhani about 8 yearsI have directly created a calendar event through my app and added reminder also. They are working perfectly. But when I click reminder notification, it's opening device calendar event. Now I want to customize reminder notification to load my app without loading default calendar. Can I achieve this ?
-
Newbie almost 8 years@Alexandre What is CALENDAR_URI_BASE in removeEvent & updateEvent methods?
-
Amardeep Kumar over 7 yearsUri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values); Throws SqliteException on M
-
Suraj Pohwani over 7 years@Amardeep did you find the solution for SqliteException?
-
Amardeep Kumar over 7 years@SurajPohwani We have to find the primary calendar first instead of using 1 as CALENDAR_ID. Check stackoverflow.com/questions/40073427/…
-
user924 about 4 yearsyou aren't using intents, you need WRITE permission for that
-
user924 about 4 years@Singed did you find solution using intent?
-
Singed about 4 years@user924 no, sorry - this was 5 years ago I don't even remember