Android calendar application integrated development example

Source: Internet
Author: User

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:

 
 
  1. <uses-permission 
  2.   android:name="android.permission.READ_CALENDAR"> 
  3.   </uses-permission> 
  4. <uses-permission 
  5.    android:name="android.permission.WRITE_CALENDAR"> 
  6.    </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:

 
 
  1. String[] projection = new String[] { "_id", "name" };  
  2. Uri calendars = Uri.parse("content://calendar/calendars");  
  3.        
  4. Cursor managedCursor =  
  5.   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:

 
 
  1. String[] projection = new String[] { "_id", "name" };  
  2. Uri calendars = Uri.parse("content://calendar/calendars");  
  3.        
  4. Cursor managedCursor =  
  5.    managedQuery(calendars, projection,  
  6.    "selected=1", null, null);  
  7.  

Now we get a list of calendars. The following code lists the query results:

 
 
  1. if (managedCursor.moveToFirst()) {  
  2.  String calName;   
  3.  String calId;   
  4.  int nameColumn = managedCursor.getColumnIndex("name");   
  5.  int idColumn = managedCursor.getColumnIndex("_id");  
  6.  do {  
  7.     calName = managedCursor.getString(nameColumn);  
  8.     calId = managedCursor.getString(idColumn);  
  9.  } 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:

 
 
  1. 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:

 
 
  1. 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.

 
 
  1. event.put("title", "Event Title");  
  2. event.put("description", "Event Desc");  
  3. 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:

 
 
  1. long startTime = START_TIME_MS;  
  2. long endTime = END_TIME_MS;  
  3. event.put("dtstart", startTime);  
  4. event.put("dtend", endTime); 

If we need to add a birthday or holiday, we can set this entry as a full-day event:

 
 
  1. 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 ):

 
 
  1. 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):

 
 
  1. event.put("visibility", 0); 

You can control whether the event triggers an alarm:

 
 
  1. 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:

 
 
  1. Uri eventsUri = Uri.parse("content://calendar/events");  
  2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.