ContentProvider示例-Calendar Provider

来源:互联网 发布:华讯网络与华三 编辑:程序博客网 时间:2024/06/09 21:52

The Calender Provider API can be used by applications and sync adapters.

Here are the rules for inserting a new event:

  • You must include CALENDAR_ID and DTSTART.
  • You must include an EVENT_TIMEZONE. To get a list of the system's installed time zone IDs, usegetAvailableIDs(). Note that this rule does not apply if you're inserting an event through the INSERTIntent, described in Using an intent to insert an event—in that scenario, a default time zone is supplied.
  • For non-recurring events, you must include DTEND.
  • For recurring events, you must include a DURATION in addition to RRULE or RDATE. Note that this rule does not apply if you're inserting an event through the INSERT Intent, described in Using an intent to insert an event—in that scenario, you can use an RRULE in conjunction with DTSTART and DTEND, and the Calendar application converts it to a duration automatically.
Calendar beginTime = Calendar.getInstance();beginTime.set(2012, 0, 19, 7, 30);Calendar endTime = Calendar.getInstance();endTime.set(2012, 0, 19, 8, 30);Intent intent = new Intent(Intent.ACTION_INSERT)        .setData(Events.CONTENT_URI)        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())        .putExtra(Events.TITLE, "Yoga")        .putExtra(Events.DESCRIPTION, "Group class")        .putExtra(Events.EVENT_LOCATION, "The gym")        .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)        .putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");startActivity(intent);
static Uri asSyncAdapter(Uri uri, String account, String accountType) {    return uri.buildUpon()        .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")        .appendQueryParameter(Calendars.ACCOUNT_NAME, account)        .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build(); }

Here is an example that shows how to open the Calendar to a particular date:

// A date-time specified in milliseconds since the epoch.long startMillis;...Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();builder.appendPath("time");ContentUris.appendId(builder, startMillis);Intent intent = new Intent(Intent.ACTION_VIEW)    .setData(builder.build());startActivity(intent);

Here is an example that shows how to open an event for viewing:

long eventID = 208;...Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);Intent intent = new Intent(Intent.ACTION_VIEW)   .setData(uri);startActivity(intent);

Here is an example of an intent that sets a new title for a specified event and lets users edit the event in the Calendar.

long eventID = 208;Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);Intent intent = new Intent(Intent.ACTION_EDIT)    .setData(uri)    .putExtra(Events.TITLE, "My New Title");startActivity(intent);
0 0
原创粉丝点击