下面是一个使用Java CalendarProvider类的示例:
```
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
public class CalendarProviderExample {
private static final String CALENDAR_NAME = "My Calendar";
public static void createCalendar(ContentResolver contentResolver) {
ContentValues contentValues = new ContentValues();
contentValues.put(CalendarContract.Calendars.ACCOUNT_NAME, "your_account_name");
contentValues.put(CalendarContract.Calendars.ACCOUNT_TYPE, "your_account_type");
contentValues.put(CalendarContract.Calendars.NAME, CALENDAR_NAME);
contentValues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CALENDAR_NAME);
contentValues.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
contentValues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
contentValues.put(CalendarContract.Calendars.OWNER_ACCOUNT, "your_owner_account");
contentValues.put(CalendarContract.Calendars.VISIBLE, 1);
contentValues.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "your_account_name")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "your_account_type")
.build();
contentResolver.insert(calendarUri, contentValues);
}
public static void deleteCalendar(ContentResolver contentResolver) {
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
calendarUri = calendarUri.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, "your_account_name")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "your_account_type")
.build();
String selection = CalendarContract.Calendars.NAME + "=?";
String[] selectionArgs = new String[]{CALENDAR_NAME};
contentResolver.delete(calendarUri, selection, selectionArgs);
}
public static void queryCalendar(ContentResolver contentResolver) {
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.ACCOUNT_TYPE,
CalendarContract.Calendars.NAME,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.CALENDAR_COLOR,
CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,
CalendarContract.Calendars.OWNER_ACCOUNT,
CalendarContract.Calendars.VISIBLE,
CalendarContract.Calendars.SYNC_EVENTS
};
String selection = CalendarContract.Calendars.NAME + "=?";
String[] selectionArgs = new String[]{CALENDAR_NAME};
Cursor cursor = contentResolver.query(calendarUri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
do {
long id = cursor.getLong(cursor.getColumnIndex(CalendarContract.Calendars._ID));
String accountName = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.ACCOUNT_NAME));
String accountType = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.ACCOUNT_TYPE));
String name = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.NAME));
String displayName = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME));
int color = cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_COLOR));
int accessLevel = cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL));
String ownerAccount = cursor.getString(cursor.getColumnIndex(CalendarContract.Calendars.OWNER_ACCOUNT));
int visible = cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars.VISIBLE));
int syncEvents = cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars.SYNC_EVENTS));
// 输出日历信息
System.out.println("ID: " + id);
System.out.println("Account Name: " + accountName);
System.out.println("Account Type: " + accountType);
System.out.println("Name: " + name);
System.out.println("Display Name: " + displayName);
System.out.println("Color: " + color);
System.out.println("Access Level: " + accessLevel);
System.out.println("Owner Account: " + ownerAccount);
System.out.println("Visible: " + visible);
System.out.println("Sync Events: " + syncEvents);
} while (cursor.moveToNext());
cursor.close();
}
}
}
```
在上面的示例中,我们首先定义了一个常量`CALENDAR_NAME`,它表示要创建和查询的日历的