One of the popular apps on Android device is the Calendar application. As one of the android app development philosophy is to make use of functionalities of other apps, it is expected by many developers that we can access the calendar data easily. However, the truth is NO!
Android doesn’t provide an official API for read/write Calendar data, probably because Android is going to change the calendar data format in future release.
The good news is there’re workarounds, more than one. This tutorial will list out those methods and provide an example at the end of the tutorial.
1. Google Calendar APIs
Google doesn’t provide Calendar API specifically for Android, but it does provide an Calendar API for more general use. The API supports .NET, Java and Python, so it is expected that it can be used in Android.
However, this method requires the app to establish a network connection through Internet (probably requires some kind of authentication), and then access the calendar data. As android already has Calendar app built in, it sounds dumb that we’ll need to connect to Internet, create a connection and access the data.
This approach is not explored in detail here. Interested readers could refer reference 2 for more details.
2. Android Calendar Private APIs
Android is open source, so it’s not difficult to find out that android actually has undocumented APIs for calendar (it’s just not public APIs).
This part will cover how to get list of calendars, how to read event, and how to add event.
2.0 Permission Declaration
In order to read calendar data, one needs to declare the “READ_CALENDAR” permission in AndroidManifest.xml file.
For write to Calendar, there’s a another “WRITE_CALENDAR” permission.
You can refer to the complete code at the end of the tutorial for exact position of the declaration.
2.1 List Calendars
One may have multiple calendars created in Calendar application. For me, I have the default calendar, a Singapore Holiday Calendar, and a third calendar from my other Gmail account.
The code to get a list of all calendars are as below,
The code essentially queries the android calendar content provider and goes through the calendar list returned. The name and calendar id are stored in m_calendars.
For android version less than 8, the uri for query calendars is content://calendar/*, while content://com.android.calendar/* is used for android version 8 and above.
2.2 Read Events
To read a list of Calendar events, one can create another query as illustrated below,
The method above get the last three events from the selected calendar. Note that m-selectedCalendarId variable and getDateTimeStr() method are defined by us. You can refer to code at the end of the tutorial for a complete picture.
2.3 Create an Event
Similarly, one can insert a new event into Calendar database.
The code above simply create a ContentValues and insert it into the database URI.
3. Sending Intent to Calendar
This method doesn’t require the READ/WRITE_CALENDAR permission. It sends out an intent to calendar, and users will be redirected to Calendar app to create the event. Note that this is not a documented method either.
Below is a method that adds an event to Android Calendar.
This method is used in my android app, Advanced Phone Log.
4. A Complete and Runnable Example
Below are screenshots of the app.
Figure 1. Screenshots of Calendar Sample App
1. Working with the Android Calendar: http://www.developer.com/ws/article.php/3850276/Working-with-the-Android-Calendar.htm
2. Google Calendar APIs and Tools: http://code.google.com/apis/calendar/data/2.0/developers_guide.html
- Android Apps (19)
- Android Tutorial (31)
- Code Snippet (2)
- Coding Beyond Technique (19)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (32)
- Data Structure & Algorithms (31)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Linux Study Notes (40)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- roman10 on Android FileObserver–The Underlying inotify Mechanism and an Example
- Chapola on How to Build ffmpeg with NDK r9
- mohan on How to Build ffmpeg with NDK r9
- Salman on How to Build ffmpeg with NDK r9
- steveli on How to Build ffmpeg with NDK r9
- June 2014 (1)
- May 2014 (2)
- October 2013 (1)
- August 2013 (4)
- May 2013 (2)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)