Android calendar provider (2)

Source: Internet
Author: User

Calendar tables

The calendarcontract. Calendars table contains details about a single calendar. The calendar columns in the following table are writable for applications and synchronization adapters. For the complete list of fields supported by this table, see "calendarcontract. Calendars reference"

Http://developer.android.com/reference/android/provider/CalendarContract.Calendars.html

Constant

Description

Name

Calendar name

Calendar_display_name

Name displayed to the user

Visible

A boolean value indicating whether the selected calendar is displayed. 0 indicates that the events associated with the calendar should not be displayed, and 1 indicates that the events associated with the calendar should be displayed. This value affects the generation of rows in the calendarcontract. Instances table.

Sync_events

A boolean value indicating whether the calendar should be synchronized and its events should be saved on the device. 0 indicates that the calendar is not synchronized and the event is saved on the device. 1. Synchronize the calendar and save its events on the device.

 

Query calendar

This is an example of how to obtain all calendars for a specific user. For simplicity and clarity, the query operation is written in the user interface thread ("main thread") in this column "). In practice, asynchronous threads should be used to replace the main thread.

// Projection array. Creating indices for this array instead of doing
// Dynamic lookups improves performance.
Public static final string [] event_projection = new string [] {
Calendars. _ id, // 0
Calendars. account_name, // 1
Calendars. calendar_display_name // 2
};

// The indices for the projection array above.
Private Static final int projection_id_index = 0;
Private Static final int projection_account_name_index = 1;
Private Static final int projection_display_name_index = 2;

Next we need to construct a query. The selection variable specifies the query conditions. In this example, find that all account_names are "sampleuser@google.com" and account_type is the calendar of "com. Google. The query returns a cursor object, which you can use to traverse the database query results.

// Run Query
Cursor cur = NULL;
Contentresolver Cr = getcontentresolver ();
Uri uri = calendars. content_uri;

String selection = "(" + calendars. account_name + "= ?) And ("

+ Calendars. account_type + "= ?)) ";

String [] selectionargs = new string [] {"sampleuser@gmail.com", "com. Google "};

// Submit the query and get a cursor object back.

Cur = Cr. Query (Uri, event_projection, selection, selectionargs, null );

Next, we use a cursor to traverse the result set and a constant to return the value of each field:

// Use the cursor to step through the returned records
While (cur. movetonext ()){
Long Calid = 0;
String displayname = NULL;
String accountname = NULL;

// Get the Field Values
Calid = cur. getlong (projection_id_index );
Displayname = cur. getstring (projection_display_name_index );
Accountname = cur. getstring (projection_account_name_index );

// Do something with the values...

...
}

Modify calendar

You can use the _ ID of the calendar to perform the update process. This ID can be either the ID appended to the URI (using the withappendedid () method) or the ID of the first selected project. The selection variable application uses "_ id = ?" And the first parameter of the selectionarg array should be the _ ID of the calendar. You can also use the encoding ID in the URI for update. The following example uses the withappendedid () method to change the display name of the calendar:

Private Static final string debug_tag = "myactivity ";
...
Long Calid = 2;
Contentvalues values = new contentvalues ();
// The New Display name for the calendar
Values. Put (calendars. calendar_display_name, "Trevor's calendar ");
Uri updateuri = contenturis. withappendedid (calendars. content_uri, Calid );
Int rows = getcontentresolver (). Update (updateuri, values, null, null );
Log. I (debug_tag, "rows updated:" + rows );

Insert calendar

Calendars are designed to manage tables by means of synchronization adapters. Therefore, you should only use synchronization adapters to insert new calendars. In most cases, the application can only change the appearance of the calendar, such as changing the display name. If the application needs to create a local calendar, it can use the account_type column of account_type_local to complete the synchronization by executing the calendar insertion process of the synchronization adapter. Account_type_local is a special calendar Account type, which is not associated with the device account. This type of calendar is not synchronized to the server. For more information about the synchronization adapter, see "synchronization adapter ".

Events table

The calendarcontract. Events table contains detailed information about a single event. To add, update, or delete an event, the application must have the write_calendar permission in its inventory file.

The following events columns are writable through applications and synchronization adapters. For the complete Field List of this table, see the calendarcontract. Events reference.

Constant

Description

Calendar_id

_ ID of the calendar to which the event belongs

Organizer

Email of the event organizer (owner)

Title

Event title

Event_location

Event Location

Description

Event Description

Dtstart

The start time of the event, in UTC millisecond time starting from the epoch.

Dtend

End Time of the event, in UTC millisecond time starting from the epoch

Event_timezone

Time zone for the event

Event_end_timezone

Time zone for event end time

Duration

The duration of an event in rfc5545 format. For example, "pt1h" indicates that the event lasts for 1 hour. "P2w" indicates the duration of 2 weeks.

All_day

1 indicates that this event will take full time (defined by the local time zone); 0 indicates that it is a common event and can start and end at any time of the day.

Rrule

Formatted event recurrence rule (rfc5545 ). For example, "freq = weekly; Count = 10; wkst = Su ".

Rdate

The recurrence date of the event. Generally, rdate must be used together with rrule to define a collection of repeated events.

Availability

If this event counts as busy time or is free time that can be scheduled over .????

Guests_can_modify

Can participants modify events?

Guests_can_invite_others

Can participants invite other participants?

Guests_can_see_guests

Can participants see the attendee list?

 

Note: rfc5545 address: http://tools.ietf.org/html/rfc5545#section-3.8.2.5

Add data to the events table

When your application needs to insert a new event, we recommend that you use an insert-type intent object (described in the section "insert events using intent objects ). However, if necessary, you can insert events directly. This section describes how to do this.

The following are rules for inserting a new event:

1. The calendar_id and dtstart fields must be included.

2. The event_timezone field must be included. Use the getavailableids () method to obtain the list of installed Time Zone IDs. NOTE: If an instert intent object is used to insert an event, this rule is not applicable because a default time zone is provided in the insert object scenario;

3. For non-recurring events, the dtend field must be included;

4. For repeated events, a duratiion field attached with the rrule or rdate field must be included. Note: This rule is not applicable if you insert an event using an insert-type intent object. Because in the Application Scenario of this intent object, you can combine the rrule, dtstart, and dtend fields, and the calendar application can automatically convert it into a continuous time.

The following is an example of inserting an event. For simplicity, this example is executed in the UI thread. In fact, the insert and update processes should be executed asynchronously in a background thread. For more information, see the asyncqueryhandler class.

Long Calid = 3;
Long startmillis = 0;
Long endmillis = 0;
Calendar begintime = calendar. getinstance ();
Begintime. Set (2012, 9, 14, 7, 30 );
Startmillis = begintime. gettimeinmillis ();
Calendar endtime = calendar. getinstance ();
Endtime. Set (2012, 9, 14, 8, 45 );
Endmillis = endtime. gettimeinmillis ();
...

Contentresolver Cr = getcontentresolver ();
Contentvalues values = new contentvalues ();
Values. Put (events. dtstart, startmillis );
Values. Put (events. dtend, endmillis );
Values. Put (events. title, "Jazzercise ");
Values. Put (events. Description, "group workout ");
Values. Put (events. calendar_id, Calid );
Values. Put (events. event_timezone, "America/los_angeles ");
Uri uri = Cr. insert (events. content_uri, values );

// Get the event ID that is the last element in the URI
Long eventid = long. parselong (URI. getlastpathsegment ());
//
//... Do something with event ID
//
//

Note: This example shows how to obtain the event ID after an event is created. This is the easiest way to obtain the event ID, you often need this event ID to perform other calendar operations, such as adding attendees or reminders to the event.

Update events

When your application wants to allow users to edit an event, we recommend that you use an intent object of the edit type. However, if necessary, you can directly edit the event. You can provide the _ ID of the event to be edited to perform event update processing. This ID can be the ID appended to the URI (using the withappendedid () method ), it can also be the first option. The selection variable should use "_ id = ?" And the first value of the selectionarg parameter must be the _ id value of the event. You can also use the selection variable without ID for update. The following example updates the title of the event specified using the withappendedid () method.

Private Static final string debug_tag = "myactivity ";
...
Long eventid = 188;
...
Contentresolver Cr = getcontentresolver ();
Contentvalues values = new contentvalues ();
Uri updateuri = NULL;
// The New title for the event
Values. Put (events. title, "Kickboxing ");
Myuri = contenturis. withappendedid (events. content_uri, eventid );
Int rows = getcontentresolver (). Update (updateuri, values, null, null );
Log. I (debug_tag, "rows updated:" + rows );

Delete data from the events table

You can use the _ id appended to the URI to delete an event or use standard selection conditions to delete the event. You cannot select an additional ID. There are two versions deleted: the application method and the synchronization adapter method. When the application is deleted, the "deleted" column is set to 1, which indicates that the synchronization adapter has been deleted and the deletion should be passed to the server. The synchronization adapter deletes the event from the database together with its associated data. The following is an example of how an application deletes an event through Event _ ID:

Private Static final string debug_tag = "myactivity ";
...
Long eventid = 201;
...
Contentresolver Cr = getcontentresolver ();
Contentvalues values = new contentvalues ();
Uri deleteuri = NULL;
Deleteuri = contenturis. withappendedid (events. content_uri, eventid );
Int rows = getcontentresolver (). Delete (deleteuri, null, null );
Log. I (debug_tag, "rows deleted:" + rows );

Attendees table

Each row of the calendarcontract. Attendees table represents a single participant of an event. Call the query () method with the given event_id to list the participants corresponding to this event. Event_id must match a special event.

 

The following table lists the writable fields in the attendees table. When a new attendee is inserted, all fields other than attendee_name must be included.

Constant

Description

Event_id

Event ID

Attendee_name

Attendee name

Attendee_email

Attendee email address

Attendee_relationship

The relationship between participants and events. One of the following values:

1. relationship_attendee

2. relationship_none

3. relationship_organizer

4. relationship_policmer

5. relationship_speaker

Attendee_type

Attendee type. One of the following values

1. type_required

2. type_optional

Attendee_status

The status of attendees. One of the following values:

1. attendee_status_accepted

2. attendee_status_declined

3. attendee_status_invited

4. attendee_status_none

5. attendee_status_tentative

 

Add attendees

The following is an example of adding an attendee to an event. Note that event_id is required:

Long eventid = 202;
...
Contentresolver Cr = getcontentresolver ();
Contentvalues values = new contentvalues ();
Values. Put (attendees. attendee_name, "Trevor ");
Values. Put (attendees. attendee_email, "trevor@example.com ");
Values. Put (attendees. attendee_relationship, attendees. relationship_attendee );
Values. Put (attendees. attendee_type, attendees. type_optional );
Values. Put (attendees. attendee_status, attendees. attendee_status_invited );
Values. Put (attendees. event_id, eventid );
Uri uri = Cr. insert (attendees. content_uri, values );

Reminders table

Each row in the calendarcontract. Reminders table represents an event reminder. Calling the query () method with the given event_id will return the reminder list for this event.

The following table lists the writable fields in the reminders table. When a new reminder is inserted, all these fields must be included. Note that the synchronization adapter specifies the supported reminder types in the calendarcontract. Calendars table. For details, see allowed_reminders.

Http://developer.android.com/reference/android/provider/CalendarContract.CalendarColumns.html#ALLOWED_REMINDERS

Constant

Description

Event_id

Event ID

Minutes

Events should be triggered several minutes ago.

Method

One of the following settings is the alarm method set on the service:

1. method_alert

2. method_default

3. method_email

4. method_sms

 

Add reminder

The following example adds a reminder to an event, which is triggered 15 minutes before the event occurs.

Long eventid = 221;
...
Contentresolver Cr = getcontentresolver ();
Contentvalues values = new contentvalues ();
Values. Put (reminders. minutes, 15 );
Values. Put (reminders. event_id, eventid );
Values. Put (reminders. method, reminders. method_alert );
Uri uri = Cr. insert (reminders. content_uri, values );

Instances table

The calendarcontract. Instances table saves the start time and end time of an event. Each row in the table represents a single event. The instances table is not writable and only provides a method to query events.

The following table lists the fields that you can query. Note: The time zone is defined by the key_timezone_type and key_timezone_instances fields.

Constant

Description

Begin

The start time of the event instance. UTC in milliseconds

End

The end time of the event instance. UTC in milliseconds

End_day

The end day of the event instance, relative to the time zone of the calendar

End_minute

Number of minutes after the event instance is calculated from 0 in the time zone of the calendar

Event_id

Event ID of this event instance

Start_day

Start day of the event instance relative to the calendar time zone

Start_minute

The number of minutes that start from 0 when compared to the calendar time zone.

Query the instances table

To query the instances table, you must specify a time range for the query in the URI. In this example, the calendarcontract. Instances class obtains access to the title field through the implementation of the calendarcontract. eventscolumns interface. In other words, the title field is returned through a database view, rather than by querying the calendarcontract. Instances table.

Private Static final string debug_tag = "myactivity ";
Public static final string [] instance_projection = new string [] {
Instances. event_id, // 0
Instances. Begin, // 1
Instances. Title // 2
};

// The indices for the projection array above.
Private Static final int projection_id_index = 0;
Private Static final int projection_begin_index = 1;
Private Static final int projection_title_index = 2;
...

// Specify the date range you want to search for recurring
// Event instances
Calendar begintime = calendar. getinstance ();
Begintime. Set (2011, 9, 23, 8, 0 );
Long startmillis = begintime. gettimeinmillis ();
Calendar endtime = calendar. getinstance ();
Endtime. Set (2011, 10, 24, 8, 0 );
Long endmillis = endtime. gettimeinmillis ();

Cursor cur = NULL;
Contentresolver Cr = getcontentresolver ();

// The ID of the recurring event whose instances you are searching
// For in the instances table
String selection = instances. event_id + "=? ";
String [] selectionargs = new string [] {"207 "};

// Construct the query with the desired date range.
Uri. Builder = instances. content_uri.buildupon ();
Contenturis. appendid (builder, startmillis );
Contenturis. appendid (builder, endmillis );

// Submit the query
Cur = Cr. Query (builder. Build (),
Instance_projection,
Selection,
Selectionargs,
Null );

While (cur. movetonext ()){
String title = NULL;
Long eventid = 0;
Long beginval = 0;

// Get the Field Values
Eventid = cur. getlong (projection_id_index );
Beginval = cur. getlong (projection_begin_index );
Title = cur. getstring (projection_title_index );

// Do something with the values.
Log. I (debug_tag, "Event:" + title );
Calendar calendar = calendar. getinstance ();
Calendar. settimeinmillis (beginval );
Dateformat formatter = new simpledateformat ("mm/DD/YYYY ");
Log. I (debug_tag, "date:" + formatter. Format (calendar. gettime ()));
}
}

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.