申請(qǐng)動(dòng)態(tài)權(quán)限
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
日歷的content Provider的uri
private static String CALENDER_URL = "content://com.android.calendar/calendars";
private static String CALENDER_EVENT_URL = "content://com.android.calendar/events";
private static String CALENDER_REMINDER_URL = "content://com.android.calendar/reminders";
添加日歷行程
解題思路:通過(guò)ContentProvider向系統(tǒng)日歷中添加數(shù)據(jù)
日歷提供程序是用戶日歷事件的存儲(chǔ)區(qū)链瓦。您可以利用 Calendar Provider API 對(duì)日歷、事件杰赛、參加者、提醒等執(zhí)行查詢侧甫、插入啥纸、更新和刪除操作。
我們先來(lái)看一下Android jar包有關(guān)于日歷提供的Provider
CalendarContract定義了日歷和日程事件相關(guān)的數(shù)據(jù)模型:
CalendarContract.Calendars :此表儲(chǔ)存日歷特定信息赶掖。 此表中的每一行都包含一個(gè)日歷的詳細(xì)信息忿危,例如名稱达箍、顏色、同步信息等铺厨。
CalendarContract.Events :此表儲(chǔ)存事件特定信息缎玫。 此表中的每一行都包含一個(gè)事件的信息 — 例如事件標(biāo)題、地點(diǎn)解滓、開始時(shí)間赃磨、結(jié)束時(shí)間等。 事件可一次性發(fā)生洼裤,也可多次重復(fù)發(fā)生邻辉。參加者、提醒和擴(kuò)展屬性存儲(chǔ)在單獨(dú)的表內(nèi)腮鞍。它們各自具有一個(gè) EVENT_ID恩沛,用于引用 Events 表中的 _ID。
CalendarContract.Instances : 此表儲(chǔ)存每個(gè)事件實(shí)例的開始時(shí)間和結(jié)束時(shí)間缕减。 此表中的每一行都表示一個(gè)事件實(shí)例。 對(duì)于一次性事件芒珠,實(shí)例與事件為 1:1 映射桥狡。對(duì)于重復(fù)事件,會(huì)自動(dòng)生成多個(gè)行皱卓,分別對(duì)應(yīng)多個(gè)事件實(shí)例裹芝。
CalendarContract.Attendees : 此表儲(chǔ)存事件參加者(來(lái)賓)信息。 每一行都表示事件的一位來(lái)賓娜汁。 它指定來(lái)賓的類型以及事件的來(lái)賓出席響應(yīng)嫂易。
CalendarContract.Reminders :此表儲(chǔ)存提醒/通知數(shù)據(jù)。 每一行都表示事件的一個(gè)提醒掐禁。一個(gè)事件可以有多個(gè)提醒怜械。 每個(gè)事件的最大提醒數(shù)量在 MAX_REMINDERS 中指定颅和,后者由擁有給定日歷的同步適配器設(shè)置。 提醒以事件發(fā)生前的分鐘數(shù)形式指定缕允,其具有一個(gè)可決定用戶提醒方式的方法峡扩。
Claendar常量:(日歷表)
Calendar的必須添加常量:
ACCOUNT_NAME :The account that was used to sync the entry to the device.
ACCOUNT_TYPE :The type of the account that was used to sync the entry to the device.
NAME:The name of the calendar. Column name.
CALENDAR_DISPLAY_NAME :The display name of the calendar. Column name.
CALENDAR_COLOR :The color of the calendar. This should only be updated by the sync adapter, not other apps, as changing a calendar's color can adversely affect its display.
CALENDAR_ACCESS_LEVEL :The level of access that the user has for the calendar
OWNER_ACCOUNT:The owner account for this calendar, based on the calendar feed. This will be different from the _SYNC_ACCOUNT for delegated calendars. Column name.
對(duì)于給定賬戶,只有在同時(shí)指定account_name和account_type時(shí)才能將其視為唯一賬戶障本,account_type字符串對(duì)應(yīng)于AccountManager出注冊(cè)賬戶時(shí)的賬戶驗(yàn)證器教届。
Calendar的建議添加常量:
SYNC_EVENTS :Is this calendar synced and are its events stored on the device? 0 - Do not sync this calendar or store events for this calendar. 1 - Sync down events for this calendar.
CALENDAR_TIME_ZONE:The time zone the calendar is associated with.
ALLOWED_REMINDERS :A comma separated list of reminder methods supported for this calendar in the format "#,#,#".
ALLOWED_AVAILABILITY :A comma separated list of availability types supported for this calendar in the format "#,#,#".
ALLOWED_ATTENDEE_TYPES :A comma separated list of attendee types supported for this calendar in the format "#,#,#".
Event常量:(事件表,所屬日歷表)
CALENDAR_ID:事件所屬日歷的id
ORGANIZER:事件組織者(所有者)的電子郵件驾霜。
TITLE:事件的標(biāo)題案训。
EVENT_LOCATION:事件的發(fā)生地點(diǎn)。
DESCRIPTION:事件的描述
DTSTART:事件開始時(shí)間粪糙,以從公元紀(jì)年開始計(jì)算的協(xié)調(diào)世界時(shí)毫秒數(shù)表示强霎。
DTEND:事件結(jié)束時(shí)間,以從公元紀(jì)年開始計(jì)算的協(xié)調(diào)世界時(shí)毫秒數(shù)表示猜旬。
EVENT_TIMEZONE:事件的時(shí)區(qū)脆栋。
EVENT_END_TIMEZONE:事件結(jié)束時(shí)間的時(shí)區(qū)。
DURATION:RFC5545式的事件持續(xù)時(shí)間洒擦。例如椿争,值為 "PT1H" 表示事件應(yīng)持續(xù)一小時(shí),值為 "P2W" 表示持續(xù) 2 周熟嫩。
ALL_DAY:值為 1 表示此事件占用一整天(按照本地時(shí)區(qū)的定義)秦踪。 值為 0 表示它是常規(guī)事件,可在一天內(nèi)的任何時(shí)間開始和結(jié)束掸茅。
RRULE: 事件的重復(fù)發(fā)生規(guī)則格式椅邓。例如,"FREQ=WEEKLY;COUNT=10;WKST=SU"昧狮。
RDATE:事件的重復(fù)發(fā)生日期,RRULE和RDATE通常聯(lián)合用于定義一組聚合重復(fù)實(shí)例景馁,具體參考RFC5545規(guī)范
AVAILABILITY:將此事件視為忙碌時(shí)間還是可調(diào)度的空閑時(shí)間。
GUESTS_CAN_MODIFY :來(lái)賓是否可修改事件逗鸣。
GUESTS_CAN_INVITE_OTHERS :來(lái)賓是否可邀請(qǐng)其他來(lái)賓合住。
GUESTS_CAN_SEE_GUESTS:來(lái)賓是否可查看參加者列表。
Reminder常量 :(提醒表撒璧,所屬事件表)
- EVENT_ID: 事件id
- MINUTES: 事件發(fā)生前的分鐘數(shù)透葛,應(yīng)在達(dá)到該時(shí)間時(shí)發(fā)出提醒
-
METHOD: 提醒方式,有:
METHOD_ALERT, METHOD_DEFAULT,
METHOD_EMAIL, METHOD_SMS
Instances常量:(實(shí)例表卿樱,所屬事件表僚害,與事件一對(duì)一映射關(guān)系,詳細(xì)解析見上文)
- BEGIN: 實(shí)例的開始時(shí)間繁调,以協(xié)調(diào)世界時(shí)毫秒數(shù)表示萨蚕。
- END: 實(shí)例的結(jié)束時(shí)間靶草,以協(xié)調(diào)世界時(shí)毫秒數(shù)表示。
- START_DAY: 與日歷時(shí)區(qū)相應(yīng)的實(shí)例儒略歷開始日门岔。
- END_DAY: 與日歷時(shí)區(qū)相應(yīng)的實(shí)例儒略歷結(jié)束日爱致。
- START_MINUTE: 從日歷時(shí)區(qū)午夜開始計(jì)算的實(shí)例開始時(shí)間(分鐘)。
- END_MINUTE: 從日歷時(shí)區(qū)午夜開始計(jì)算的實(shí)例結(jié)束時(shí)間(分鐘)寒随。
- EVENT_ID: 該實(shí)例對(duì)應(yīng)事件的 _ID糠悯。
向系統(tǒng)Calendar中添加Event
public class CalendarProviderUtil {
// ContentProvider的uri
private static Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
private static Uri eventUri = CalendarContract.Events.CONTENT_URI;
private static Uri reminderUri = CalendarContract.Reminders.CONTENT_URI;
private static ContentResolver contentResolver;
/**
* 檢查是否有日歷表,有返回日歷id,沒(méi)有-1
* */
private static int isHaveCalender(){
// 查詢?nèi)諝v表的cursor
Cursor cursor = contentResolver.query(calendarUri,null,null,null,null);
if (cursor == null || cursor.getCount() == 0){
return -1;
}else {
// 如果有日歷表
try {
cursor.moveToFirst();
// 通過(guò)cursor返回日歷表的第一行的屬性值 第一個(gè)日歷的id
return cursor.getInt(cursor.getColumnIndex(CalendarContract.Calendars._ID));
}finally {
cursor.close();
}
}
}
/**
* 添加日歷表
* */
private static long addCalendar(){
// 時(shí)區(qū)
TimeZone timeZone = TimeZone.getDefault();
// 配置Calendar
ContentValues value = new ContentValues();
value.put(CalendarContract.Calendars.NAME, "我的日歷表");
value.put(CalendarContract.Calendars.ACCOUNT_NAME, "myAccount");
value.put(CalendarContract.Calendars.ACCOUNT_TYPE, "myType");
value.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "myDisplayName");
value.put(CalendarContract.Calendars.VISIBLE, 1);
value.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.BLUE);
value.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
value.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
value.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, timeZone.getID());
value.put(CalendarContract.Calendars.OWNER_ACCOUNT, "myAccount");
value.put(CalendarContract.Calendars.CAN_ORGANIZER_RESPOND, 0);
value.put(CalendarContract.CALLER_IS_SYNCADAPTER,true);
// 插入calendar
Uri insertCalendarUri = contentResolver.insert(calendarUri,value);
if (insertCalendarUri == null){
return -1;
}else {
// return Integer.parseInt(insertCalendarUri.toString());
return ContentUris.parseId(insertCalendarUri);
}
}
/**
* 添加日歷事件
* */
public static void addEvent(Context context){
// 創(chuàng)建contentResolver
contentResolver = context.getContentResolver();
// 日歷表id
int calendarId = isHaveCalender();
if (calendarId == -1){
addCalendar();
calendarId = isHaveCalender();
}
// startMillis
Calendar beginTime = Calendar.getInstance();
beginTime.set(2019,8,15);
long startMillis = beginTime.getTimeInMillis();
// endMillis
Calendar endTime = Calendar.getInstance();
endTime.set(2019,8,15);
long endMillis = endTime.getTimeInMillis();
// 準(zhǔn)備event
ContentValues valueEvent = new ContentValues();
valueEvent.put(CalendarContract.Events.DTSTART,startMillis);
valueEvent.put(CalendarContract.Events.DTEND,endMillis);
valueEvent.put(CalendarContract.Events.TITLE,"事件標(biāo)題");
valueEvent.put(CalendarContract.Events.DESCRIPTION,"事件描述");
valueEvent.put(CalendarContract.Events.CALENDAR_ID,calendarId);
valueEvent.put(CalendarContract.Events.EVENT_TIMEZONE,"Asia/Shanghai");
// 添加event
Uri insertEventUri = contentResolver.insert(eventUri,valueEvent);
if (insertEventUri == null){
Toast.makeText(context,"添加event失敗",Toast.LENGTH_SHORT).show();
}
// 添加提醒
long eventId = ContentUris.parseId(insertEventUri);
ContentValues valueReminder = new ContentValues();
valueReminder.put(CalendarContract.Reminders.EVENT_ID,eventId);
valueReminder.put(CalendarContract.Reminders.MINUTES,15);
valueReminder.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALARM);
Uri insertReminderUri = contentResolver.insert(reminderUri,valueReminder);
if (insertReminderUri == null){
Toast.makeText(context,"添加reminder失敗",Toast.LENGTH_SHORT).show();
}
}
}
只測(cè)試了一下妻往,有用互艾,可能有bug