BKJIA: Android phones generally have built-in Calendar applications. Third-party applications can use the calendar content provider interface to read user calendar information and arrange new events in the calendar. This calendar application can directly synchronize content with your Google Calendar application.
Unfortunately, there is no document to describe the integration of the calendar application on Android, or other applications similar to the Contact application. All the information in this article comes from the reverse engineering of the Google Calendar application content provider. This interface often changes and supports limited functions. However, for some types of applications, the integrated calendar application may be very powerful.
The code in this article has been tested in versions earlier than Android 2.0 SDK. Because no Android 2.0 mobile phone is available on the market, we cannot ensure that it can run on the devices to be released. We tested and ran the code on T-Mobile G1 (SDK 1.6.
Access calendar data
To add calendar support to your Android app, you must add the following code to the AndroidManifest. xml file of your app:
- <uses-permission
- android:name="android.permission.READ_CALENDAR">
- </uses-permission>
- <uses-permission
- android:name="android.permission.WRITE_CALENDAR">
- </uses-permission>
After registering these licenses, you can access your calendar data without any Google login issues. Because the calendar application is not installed in the Android simulator, all testing and development work must be performed on the real machine where the calendar application is installed, and the corresponding provider cannot be started using the simulator. Calendar applications and content providers are included in Android phones that support Google services, such as T-Mobile G1.
Retrieve the user calendar event list
Each user may have many specified schedules in the calendar application. For example, a user may have both a work schedule, a personal schedule, and a holiday schedule.
By using the content provider interface, you can access user configuration schedules. To retrieve the list of events scheduled by a user, we need to write an appropriate Uri query for the calendar content provider, as shown below:
- String[] projection = new String[] { "_id", "name" };
- Uri calendars = Uri.parse("content://calendar/calendars");
-
- Cursor managedCursor =
- managedQuery(calendars, projection, null, null, null);
Now, this query returns all schedules, including those that are not activated. To retrieve only the list of active schedules, we need to limit our query to return only the "selected" field value as a real record:
- String[] projection = new String[] { "_id", "name" };
- Uri calendars = Uri.parse("content://calendar/calendars");
-
- Cursor managedCursor =
- managedQuery(calendars, projection,
- "selected=1", null, null);
-
Now we get a list of calendars. The following code lists the query results:
- if (managedCursor.moveToFirst()) {
- String calName;
- String calId;
- int nameColumn = managedCursor.getColumnIndex("name");
- int idColumn = managedCursor.getColumnIndex("_id");
- do {
- calName = managedCursor.getString(nameColumn);
- calId = managedCursor.getString(idColumn);
- } while (managedCursor.moveToNext());
- }
Once we know which calendar we want to access, we can add a calendar event. A calendar event has many important fields, including the event question, time, and position, and how the event entries are displayed in the calendar. Calendar events can be one-time or repetitive.
Add a single event to the calendar
To add entries to a specific calendar, use ContentValues to configure a calendar entry insertion:
- ContentValues event = new ContentValues();
Each event needs to be specified to a specific calendar. Therefore, you must first insert this event using the calendar identifier:
- event.put("calendar_id", calId);
Then we set the basic information of the event, including the character segments such as the event question, description, and location.
- event.put("title", "Event Title");
- event.put("description", "Event Desc");
- event.put("eventLocation", "Event Location");
There are also many different options to configure the event time and date. The following code sets the start time of an event:
- long startTime = START_TIME_MS;
- long endTime = END_TIME_MS;
- event.put("dtstart", startTime);
- event.put("dtend", endTime);
If we need to add a birthday or holiday, we can set this entry as a full-day event:
- event.put("allDay", 1);
This information is sufficient for most entries. However, there are many other useful calendar entry attributes. For example, you can set the time status to temporary (value: 0), habitual (value: 1), or cancel (value: 2 ):
- event.put("eventStatus", 1);
You can also control who can view it and set its visibility to default (value: 0), secret (value: 1), private (value: 2), or public (value: 3):
- event.put("visibility", 0);
You can control whether the event triggers an alarm:
- event.put("hasAlarm", 1);
Once a calendar event is correctly configured, we can use ContentResolver to insert this new calendar to a calendar event with the appropriate Uri:
- Uri eventsUri = Uri.parse("content://calendar/events");
- Uri url = getContentResolver().insert(eventsUri, event);
- Scala + Eclipse + Android mobile development
- Six Reasons why Android occupies 2010
- Developer resentment: Android apps are like nightmares
- Detailed explanation of Android source code compilation
- Android developer contest champion won $0.25 million
You can call the insert () method to establish a connection with the content provider of the program table and try to insert this entry into the corresponding user calendar. If you find the calendar application and start it, you will see that your calendar entries are already in the corresponding calendar. If you use the Google online calendar service, because the calendar application has the automatic synchronization function, you will also see that this entry has already appeared in the online service.
Add a recurring event to the calendar
You can also configure recurrence calendar events. You only need to add several fields for the event in the form of a retransmission rule. The detailed description of this rule is based on RFC2445.
Android applications can work closely with calendar applications of many Android mobile users. The Calendar feature needs to be accessed through the content provider interface, which allows third-party applications to read calendar information and add new schedules.
Original article: Working with the Android Calendar Author: Shane Conder