Android的calendar是够让人纠结的。转载两篇老外的文章过来,慢慢学习
1 Calendar add event
Calendar
The calendar content provider is not officially published in the Android SDK but after searching through the Android source code I have finally figured out how to add events to calendars.
This tutorial only covers adding events to calendars. It does not cover reading events from calendars.
Before you can add an event to a calendar, you need to know which calendars exists and what their identifiers are.
Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
The code above creates an array of strings containing the names and an array of integers containing the calendar_ids of all available calendars.
To actually add an event to a calendar, start by creating a ContentValues object
Put a calendar_id from the array above in there along with the title, date and location for the event.
It should look something like this:
ContentValues cv = new ContentValues();
cv.put("calendar_id", CalIds[0]);
cv.put("title", myTitle);
cv.put("dtstart", startTime);
cv.put("dtend", endTime);
cv.put("eventLocation", myLocation);
Finally, write these values to the calendar like such:
URI newevent = cr.insert(Uri.parse("content://calendar/events"), cv);
To add a reminder to the event you need to write a record to the events table.
In that record you must refer to the id of your new event which was returned by the insert method.
get the id like this:
String eventid = newevent.getPathSegments().get(newevent.getPathSegments().size()-1);
then make a new contentvalues object with these fields
cv.put("event_id",eventid);
cv.put("minutes",interval);
and insert into the reminders table
cr.insert(Uri.parse(content://calendar/reminders"),cv);
As this content provider is not officially public, it could change in future Android versions which might break your app.
2. Calendar Read calendars
Then get an android.content.ContentResolver and make a query to content://calendar/calendars to enumerate all the available calendars
on the phone. The '_id' column will give you the calendar's ID (referenced in events). You can get rough documentation of all the columns supported by looking at core/java/android/provider/Calendar.java. (As mentioned previously, this file is part of the public open source Android project but is not part of the official Android SDK from Google)
ContentResolver contentResolver = context.getContentResolver();
final Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected" }), null, null, null);
while (cursor.moveToNext()) {
final String _id = cursor.getString(0);
final String displayName = cursor.getString(1);
final Boolean selected = !cursor.getString(2).equals("0");
System.out.println("Id: " + _id + " Display Name: " + displayName + " Selected: " + selected);
}
Once you have a calendar ID you can use your ContentResolver object to obtain the events from that calendar. Note how a 'where' clause of 'Calendars._id=??' is employed to fetch only events of that particular calendar. This clause could query whichever columns your app required, using an SQL-like syntax. Note also how the Uri.Builder was used to build up a URL including date bounds for the query.
PLAIN TEXT
JAVA:
Uri.Builder builder = Uri.parse("content://calendar/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.WEEK_IN_MILLIS);
ContentUris.appendId(builder, now + DateUtils.WEEK_IN_MILLIS);
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "title", "begin", "end", "allDay"}, "Calendars._id=" + id,
null, "startDay ASC, startMinute ASC");
while (eventCursor.moveToNext()) {
final String title = eventCursor.getString(0);
final Date begin = new Date(eventCursor.getLong(1));
final Date end = new Date(eventCursor.getLong(2));
final Boolean allDay = !eventCursor.getString(3).equals("0");
System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
" All Day: " + allDay);
}
Note that all dates in the calendar format are stored as UTC (the number of milliseconds that have elapsed between midnight 1st January 1970 and the event in Greenwich UK).
This is the basics of calendar access on Android. I haven't covered all the columns (these can be seen in the SDK source linked above) and I haven't covered modification. Notheless I hope this information allows other developers to build great calendar-aware apps for Android. Please do leave queries in the comments here, and if you use this information to make an app please do tell us about it in the comments here.