Building a calendar app on Android's calendar provider
androidcalendarcontractcontent-providerrecurrencerrulereminders
Android has a system calendar database, CalendarContract, that sync adapters (Google, Exchange, CalDAV) fill and write back to. A calendar app that keeps no database of its own and reads and writes only through it gets every synced account for free, and its edits sync back to the server. Justin's Calendar app (io.jhampton.calendar, Kotlin + Compose, 2026-10-27) is built that way; this is what it took.
Reading events
- Query
Instances, notEvents.Instances.CONTENT_URIwith a begin and end time appended returns every occurrence in the range with recurring events already expanded.Eventsholds one row per series. - The provider expands in local time, but all-day events are stored at UTC midnights. Query a day of margin on each side and filter in code, or an all-day event can be missing from the first or last day of a view west of UTC.
- Read an all-day occurrence's start and end as UTC dates, not local times, or it shifts a day.
- The NULL trap: to hide canceled events,
STATUS != 2is wrong. Many events haveSTATUSNULL, and in SQLNULL != 2is not true, so that filter hides them all. Use(STATUS IS NULL OR STATUS != 2). The first build hid events this way until it was caught on the emulator. - Register a
ContentObserveron the provider so the views refresh when a sync lands. VISIBLE = 1respects the user's calendar on/off choices.
Writing events
- Needs
READ_CALENDAR+WRITE_CALENDAR. - One-off events:
DTSTART+DTEND. Recurring events:DTSTART+DURATION(e.g.P3600S) +RRULE, withDTENDNULL. That is the provider's documented contract; switching an event between one-off and recurring means nulling the other pair of fields. - All-day events: midnight-to-midnight in UTC with
EVENT_TIMEZONE = "UTC". - Save new events as
STATUS_CONFIRMED. - Reminders are rows in
Reminders(EVENT_ID,MINUTES,METHOD_ALERT); setHAS_ALARMon the event. On edit, deleting and re-inserting the event's reminder rows is simplest. - Editing a recurring event this way edits the whole series; editing a single occurrence means creating an exception event, which is more work.
- Deleting one occurrence of a series: insert into
Events.CONTENT_EXCEPTION_URI/<eventId>with that occurrence'sORIGINAL_INSTANCE_TIMEandSTATUS_CANCELED. - Keep a custom
RRULEthe UI cannot represent verbatim unless the user picks a different repeat, so editing the title of an imported event does not silently rewrite its recurrence.
A calendar for people with no account
If there is no writable calendar, create one: insert into Calendars with ACCOUNT_TYPE_LOCAL, using a URI with CALLER_IS_SYNCADAPTER=true plus the account name and type as query parameters (only a "sync adapter" may create calendars). It lives only on the phone.
Reminders
The provider schedules reminders itself and broadcasts EVENT_REMINDER (data content://com.android.calendar/<alarm time>). A receiver with that action and data filter looks up the due alerts in CalendarAlerts by ALARM_TIME and posts notifications. The stock calendar app also listens, so offer a switch to turn the app's own notifications off and avoid doubles. All-day reminders count back from the start of the day: "the day before at 9 AM" is 900 minutes (15 hours).
Integration
- Handle
ACTION_INSERTonEvents(other apps' "add to calendar"),ACTION_VIEWonevents/<id>, andVIEWontime/<ms>; addAPP_CALENDARto be picked as the calendar app. - A live date on the launcher icon needs activity aliases: A launcher icon that changes (today's date on a calendar icon).
Layout math worth keeping
Day and week views place overlapping events in columns: sort by start, group events whose times chain-overlap into a cluster, and give each event the first column whose last event has ended; the cluster's column count sets the widths. Keep this pure (no Android types) so it is unit-testable.
See also
- Generating iCalendar (.ics) files and subscribe feeds
- Alarms and timers that ring on time (Android clock apps)
- A launcher icon that changes (today's date on a calendar icon)