Call the Android own calendar feature

Source: Internet
Author: User
Tags time and date

Android phones are equipped with a built-in Calendar app. Third-party applications can use the Calendar content provider interface to read the user's calendar information and schedule new events in the calendar. This calendar can be synced directly to the user's Google Calendar.

Unfortunately, there is no documentation and integration of the Android phone's calendar app because there is another contact application. Instead, all of the information provided herein will be made available through the reverse engineering Google Calendar content provider. This interface is subject to change and will support limited functionality. However, calendar integration can be a powerful feature of some types of applications.

This article tests the code to the Android 2.0 SDK version. We will release an update if there is a big shift. We did not test this device, such as the previous T-mobile G1 (SDK 1.6) code.

accessing calendar data
In order to support the Android app added to your calendar, you must add the following permissions to your application Androidmanifest.xml file:

Java code
    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>



Registering these permissions allows you to access calendar data for Google's logged-on users without having to deal with any issues. The calendar application is not installed on the Android emulator, so all testing and development must be done on the device that the calendar application actually finishes installing (using the emulator will not be able to start the appropriate provider). Calendar apps and content providers can be in the service with Google, such as T-mobile G1.
Retrieving a list of user calendars
Users may have calendars with many names configured in the Calendar application. For example, a user might have a working calendar (work calendar related activities), a family calendar (personal), and a holiday calendar (Statutory holidays).
The user-configured calendar uses and accesses the content provider interface. In order to retrieve the user's calendar list, we need to design the query for the appropriate URI and the calendar content provider as follows:

Java code
    1. string[] projection = new string[] { "_id", "name"};
    2. Uri calendars = uri.parse ("Content://calendar/calendars");
    3. Cursor Managedcursor =
    4. Managedquery (calendars, projection, null, null, null );



Now, this query will return all calendars, including those that are not normally used. In order to get an active calendar list, we need to limit our queries in areas where "select" is set to True

Java code
    1. string[] projection = new string[] { "_id", "name"};
    2. Uri calendars = uri.parse ("Content://calendar/calendars");
    3. Cursor Managedcursor =
    4. Managedquery (calendars, projection, "selected=1", null, null);


We now retrieve the list of calendars. The results we can traverse are as follows:

Java code
  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 ());
  10. }



Once we know the calendar we want to access, we can add a calendar event. Calendar events have some important areas, including the name of the event, the time and location, and the information that is set, which item will be displayed in the calendar. Calendar events may be one-time or recurring.

Add a single event to a calendar

To add an entry to a particular calendar, we need to configure a calendar entry to be used with Contentvalues as follows:

Java code
    1. Contentvalues event = new Contentvalues ();



Each activity must be combined with a specific calendar, so the first thing you'll think about is inserting a calendar identifier for this event.

Java code
    1. Event.put ("calendar_id", calid);


We then set up some basic information about the event, which includes the title of the activity, description and location of the string music field.

Java code
    1. Event.put ("title", "Event title");
    2. Event.put ("description", "Event Desc");
    3. Event.put ("eventlocation", "Event location");


There are many different configuration options to set the time and date of the event.

We can set the start and end information for the event as follows:

Java code
    1. Long startTime = Start_time_ms;
    2. long endTime = End_time_ms;
    3. Event.put ("Dtstart", startTime);
    4. Event.put ("Dtend", endTime);



If we add a birthday or holiday, we'll set an entry for an all-day event:

Java code
    1. Event.put ("AllDay", 1); //0 for False, 1 for true


This information is sufficient for most items. However, there are some other useful calendar item properties.

For example, you can set the event status tentative (0), confirm (1), or Cancel (2):

Java code
    1. Event.put ("EventStatus", 1);


You can control the visibility to which it is set to the default value (0 this event), confidential (1), Private (2), or public (3):

Java code
    1. Event.put ("visibility", 0);


You can control whether events on the calendar consume time by setting their transparency, opacity (0), or transparent (1).

Java code
    1. Event.put ("Transparency", 0);


You can control whether an event triggers an alert, as follows:

Java code
    1. Event.put ("Hasalarm", 1); //0 for False, 1 for true


Once the calendar event is configured correctly, we are ready to use Contentresolver to insert the calendar event entry into the corresponding open new calendar:

Java code
    1. Uri Eventsuri = Uri.parse ("content://calendar/events");
    2. Uri URL = getcontentresolver (). Insert (Eventsuri, event);


The call to the Insert () method touches the calendar content provider and attempts to insert the calendar entry into the appropriate user. If you navigate to the Calendar app and start it, you should see your calendar entries in the appropriate calendar. From Calendar sync, you also see calendar items online If you use Google Calendar on the web.

Add a calendar for a recurring event

You can also set recurring calendar events. To do this, you must add more fields according to the current rules. The rules are based on RFC2445.

Conclusion

Android apps can integrate with the user's calendar close to many Android devices. The calendar feature is the interface of a content provider that allows third-party applications to access calendar information and add new calendar items.

Call the Android own calendar feature

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.