Java CalendarProvider类使用实例

61次阅读
没有评论

共计 4261 个字符,预计需要花费 11 分钟才能阅读完成。

下面是一个使用 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,它表示要创建和查询的日历的

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-20发表,共计4261字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)