应用通过calendarprovider的接口熟练使用calendar后,可阅读calendarprovider来深入理解其内部实现:
CalendarProvider对应的代码在:
http://androidxref.com/9.0.0_r3/xref/packages/providers/CalendarProvider/
对应的代码只有以下几个
1.日程的添加:
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);
uri对应的是:
uri :"content://" + com.android.calendar+ "/events";
观察calendarprovider的AndroidManifest.xml:
<provider android:name="CalendarProvider2"
android:authorities="com.android.calendar"
android:label="@string/provider_label"
android:multiprocess="false"
android:exported="true"
android:readPermission="android.permission.READ_CALENDAR"
android:writePermission="android.permission.WRITE_CALENDAR" />
所以我们再添加的时候优先走到了SQLiteContentProvider的Insert方法:
之后调用了CalendarProvider2的insertInTransaction方法
然后根据uri匹配到case为Event,之后便进行了一系列的检查,最后通过CalendarDatabaseHelper的eventsInsert方法插入到event表中,然后通过CalendarInstancesHelper的updateInstancesLocked方法,将数据同步到instance表中;
2.提醒的添加:
至此,还未涉及到reminder表和alert表;
所以一般添加提醒需要在通过一下的方法再调用数据库:
ontentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Reminders.MINUTES, 15);
values.put(Reminders.EVENT_ID, eventID);
values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
Uri uri = cr.insert(Reminders.CONTENT_URI, values);
此时会走到CalendarProvider2的insertInTransaction方法对应的Reminder的case:
通过mDbHelper.remindersInsert(values);方法向reminder表中添加数据;
此处没看出来是如何更新alert表的?(scheduleNextAlarmLocked中插入的)
最后调用了:
mCalendarAlarm.checkNextAlarm(false /* do not remove alarms */);
来检查下一个需要提醒的闹钟;
网友评论