I want to add calendar events programmatically (directly) in android 4+. Is it this possible to be tested on emulator? I don't own an android phone. Some sample code would be appreciated. I read Calendar Provider of android developers but I'm confused. How can I add events to the default calendar of a user? I don't need to be synced.
EDIT: I do not want to launch an event adding Intent. Instead I want to add them completely from code and not launch another activity. I need to be able to test on an emulator that the events will be added to the main calendar of the default user of the device. How do I set up an emulator to view the default calendar of the user?
Go to Apps/Calendar (clicking on the Google icon, not the Samsung) /Permissions/allowing the calendar, contacts and location options, then, underneath the Permissions portion, click on Set as default.
I believe the section you are looking for is Using an intent to insert an event. In this section it describes how to create an intent for the event you want to add and then the default calender program on the emulator will respond and add it. You may have to set up a dummy profile so that the calendar program will start if you actually want to see that it receives the correct information.
Code from Android Dev Site:
Calendar beginTime = Calendar.getInstance(); beginTime.set(2012, 0, 19, 7, 30); Calendar endTime = Calendar.getInstance(); endTime.set(2012, 0, 19, 8, 30); Intent intent = new Intent(Intent.ACTION_INSERT) .setData(Events.CONTENT_URI) .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()) .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()) .putExtra(Events.TITLE, "Yoga") .putExtra(Events.DESCRIPTION, "Group class") .putExtra(Events.EVENT_LOCATION, "The gym") .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) .putExtra(Intent.EXTRA_EMAIL, "[email protected],[email protected]"); startActivity(intent);
Here is a working example of what i eventually made it:
ContentResolver cr = ctx.getContentResolver(); ContentValues values = new ContentValues(); values.put(CalendarContract.Events.DTSTART, dtstart); values.put(CalendarContract.Events.TITLE, title); values.put(CalendarContract.Events.DESCRIPTION, comment); TimeZone timeZone = TimeZone.getDefault(); values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID()); // Default calendar values.put(CalendarContract.Events.CALENDAR_ID, 1); values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL=" + dtUntill); // Set Period for 1 Hour values.put(CalendarContract.Events.DURATION, "+P1H"); values.put(CalendarContract.Events.HAS_ALARM, 1); // Insert event to calendar Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
where dtuntil
is
SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); Calendar dt = Calendar.getInstance(); // Where untilDate is a date instance of your choice, for example 30/01/2012 dt.setTime(untilDate); // If you want the event until 30/01/2012, you must add one day from our day because UNTIL in RRule sets events before the last day dt.add(Calendar.DATE, 1); String dtUntill = yyyyMMdd.format(dt.getTime());
Ref: Recurrence Rule
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With