Android CalDAV开发Demo
时间: 2023-10-04 09:14:15 浏览: 131
CalDAV (rfc4791)
CalDAV是一种用于在远程服务器上存储和管理日历数据的标准协议。在Android应用中使用CalDAV可以让用户方便地查看和编辑他们的日历事件,而无需离开应用程序。
下面是一个基本的Android CalDAV开发Demo,它演示了如何连接到远程CalDAV服务器并获取日历事件。
首先,您需要在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.INTERNET"/>
```
接下来,在您的应用程序中创建一个新的Java类,并添加以下代码:
```java
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class CalDAVDemo {
private static final String TAG = "CalDAVDemo";
private static final String CALENDAR_DISPLAY_NAME = "My Calendar";
private static final String CALENDAR_ACCOUNT_NAME = "myaccount@mydomain.com";
private static final String CALENDAR_ACCOUNT_TYPE = "com.example";
private static final String CALENDAR_ACCESS_LEVEL = "700";
private static final String[] CALENDAR_PROJECTION = new String[]{
Calendars._ID,
Calendars.NAME,
Calendars.ACCOUNT_NAME,
Calendars.ACCOUNT_TYPE,
Calendars.CALENDAR_DISPLAY_NAME,
Calendars.CALENDAR_COLOR,
Calendars.VISIBLE,
Calendars.OWNER_ACCOUNT
};
public static void addEvent(Context context, String title, String location, long startTime, long endTime) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
values.put(CalendarContract.Events.DTSTART, startTime);
values.put(CalendarContract.Events.DTEND, endTime);
values.put(CalendarContract.Events.CALENDAR_ID, getCalendarId(context));
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventId = Long.parseLong(uri.getLastPathSegment());
Log.d(TAG, "Event added: " + title + " (ID = " + eventId + ")");
}
public static void deleteEvent(Context context, long eventId) {
ContentResolver cr = context.getContentResolver();
Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
int rows = cr.delete(deleteUri, null, null);
Log.d(TAG, "Event deleted: " + eventId + " (" + rows + " rows deleted)");
}
public static void updateEvent(Context context, long eventId, String title, String location, long startTime, long endTime) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.EVENT_LOCATION, location);
values.put(CalendarContract.Events.DTSTART, startTime);
values.put(CalendarContract.Events.DTEND, endTime);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri updateUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
int rows = cr.update(updateUri, values, null, null);
Log.d(TAG, "Event updated: " + eventId + " (" + rows + " rows updated)");
}
public static void listEvents(Context context) {
ContentResolver cr = context.getContentResolver();
// Define the query parameters
Uri uri = CalendarContract.Events.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND
};
// Get the current time
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
// Define the query selection
String selection = "(" + CalendarContract.Events.DTSTART + ">= ?)";
String[] selectionArgs = new String[]{Long.toString(now)};
// Sort the results by start time in ascending order
String sortOrder = CalendarContract.Events.DTSTART + " ASC";
// Execute the query
Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);
// Iterate over the results and print them to the log
while (cursor.moveToNext()) {
long eventId = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events._ID));
String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE));
String location = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION));
long startTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART));
long endTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTEND));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startDate = sdf.format(new Date(startTime));
String endDate = sdf.format(new Date(endTime));
Log.d(TAG, "Event found: " + eventId + " - " + title + " (" + startDate + " to " + endDate + ")");
}
cursor.close();
}
private static long getCalendarId(Context context) {
ContentResolver cr = context.getContentResolver();
// Check if the calendar already exists
Cursor cursor = cr.query(Calendars.CONTENT_URI, CALENDAR_PROJECTION, Calendars.ACCOUNT_NAME + "=? AND " + Calendars.ACCOUNT_TYPE + "=?",
new String[]{CALENDAR_ACCOUNT_NAME, CALENDAR_ACCOUNT_TYPE}, null);
if (cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndex(Calendars._ID));
cursor.close();
return id;
}
// Create a new calendar
ContentValues values = new ContentValues();
values.put(Calendars.NAME, CALENDAR_DISPLAY_NAME);
values.put(Calendars.ACCOUNT_NAME, CALENDAR_ACCOUNT_NAME);
values.put(Calendars.ACCOUNT_TYPE, CALENDAR_ACCOUNT_TYPE);
values.put(Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_DISPLAY_NAME);
values.put(Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(Calendars.VISIBLE, 1);
values.put(Calendars.OWNER_ACCOUNT, CALENDAR_ACCOUNT_NAME);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, CALENDAR_ACCESS_LEVEL);
Uri uri = cr.insert(Calendars.CONTENT_URI, values);
long id = Long.parseLong(uri.getLastPathSegment());
Log.d(TAG, "Calendar created: " + id);
return id;
}
}
```
上述代码中的addEvent()、deleteEvent()、updateEvent()和listEvents()方法分别用于添加、删除、更新和列出日历事件。它们都需要一个上下文参数,在Android应用程序中调用时应该传递当前Activity的上下文。这些方法中的大部分代码都是直接从Android文档中复制粘贴的,因此您可以在其中找到更多有关如何使用CalDAV的信息。
在这个例子中,我们还包括了一个getCalendarId()方法,它用于检查是否已经存在一个具有指定名称和类型的日历,如果不存在则创建一个新的日历。这个方法是可选的,但如果您想让您的应用程序创建自己的日历,这是一个很好的方法。请注意,您需要修改CALDAR_ACCOUNT_NAME和CALENDAR_ACCOUNT_TYPE常量以适应您的实际情况。
最后,您可以在您的Activity中调用这些方法来实现您的应用程序的特定功能。例如,您可以在onCreate()方法中添加以下代码来添加一个新的日历事件:
```java
long startTime = System.currentTimeMillis() + 60 * 60 * 1000;
long endTime = startTime + 2 * 60 * 60 * 1000;
CalDAVDemo.addEvent(this, "Test Event", "Test Location", startTime, endTime);
```
这将在当前时间的一小时后添加一个名为“Test Event”的日历事件,持续时间为两小时。您可以运行您的应用程序并在日历应用程序中查看新事件。
阅读全文