android 日历
时间: 2023-06-30 15:03:37 浏览: 153
Android提供了一个名为“Calendar”的类,它允许您访问设备上的日历信息。您可以使用该类来执行以下操作:
1. 获取日历列表
2. 获取指定日历的事件列表
3. 创建新日历
4. 创建新事件
5. 更新或删除现有事件
以下是一些示例代码,演示如何执行这些操作:
1. 获取日历列表:
```
Calendar calendar = Calendar.getInstance();
Uri calendarsUri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.OWNER_ACCOUNT
};
Cursor cursor = getContentResolver().query(calendarsUri, projection,
null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String displayName = cursor.getString(1);
String accountName = cursor.getString(2);
String ownerName = cursor.getString(3);
// Do something with calendar information...
}
cursor.close();
```
2. 获取指定日历的事件列表:
```
Calendar calendar = Calendar.getInstance();
long calendarId = ...; // ID of the calendar you want to retrieve events for
Uri eventsUri = CalendarContract.Events.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND
};
String selection = CalendarContract.Events.CALENDAR_ID + " = ?";
String[] selectionArgs = new String[]{Long.toString(calendarId)};
Cursor cursor = getContentResolver().query(eventsUri, projection,
selection, selectionArgs, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String title = cursor.getString(1);
String description = cursor.getString(2);
long start = cursor.getLong(3);
long end = cursor.getLong(4);
// Do something with event information...
}
cursor.close();
```
3. 创建新日历:
```
Calendar calendar = Calendar.getInstance();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.NAME, "My Calendar");
values.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);
values.put(CalendarContract.Calendars.ACCOUNT_NAME, "myaccount@example.com");
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google");
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, "myaccount@example.com");
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri uri = getContentResolver().insert(CalendarContract.Calendars.CONTENT_URI, values);
long calendarId = ContentUris.parseId(uri);
```
4. 创建新事件:
```
Calendar calendar = Calendar.getInstance();
long startMillis = ...; // Start time of the event in milliseconds
long endMillis = ...; // End time of the event in milliseconds
long calendarId = ...; // ID of the calendar to add the event to
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "My Event");
values.put(CalendarContract.Events.DESCRIPTION, "This is my event description");
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
long eventId = ContentUris.parseId(uri);
```
5. 更新或删除现有事件:
```
Calendar calendar = Calendar.getInstance();
long eventId = ...; // ID of the event to update or delete
// To update an event:
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.TITLE, "New Title");
values.put(CalendarContract.Events.DESCRIPTION, "New Description");
Uri uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
int rows = getContentResolver().update(uri, values, null, null);
// To delete an event:
Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
rows = getContentResolver().delete(deleteUri, null, null);
```
阅读全文