文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java CalendarProvider类使用实例

2023-09-28 17:16

关注

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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯