在Android平板(800x600)上运行的一个日历项目,闲话不说先上图
当点击“添加”是跳转到添加事件界面
先分析主界面。
那个日历是自己画的一个View,自己定义View后面再说,也可自己去看项目代码,这里主要说一下怎样查询日历的事件。
在2.1之后
Uri CALANDER_URI = Uri.parse( "content://com.android.calendar/calendars");
在2.1之前
Uri CALANDER_URI = Uri.parse( "content://com.android.calendar/");
先贴出查询代码
Uri WHEN_URL = Uri.parse("content://com.android.calendar/instances/when");
ContentResolver contentResolver = getContentResolver();
Uri.Builder builder = WHEN_URL.buildUpon();
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(mYear, mMonth, mDay, 0, 0, 0);
long daystart = cal.getTimeInMillis();
cal.clear();
cal.set(mYear, mMonth, mDay, 23, 59, 59);
long dayend = cal.getTimeInMillis();
ContentUris.appendId(builder, daystart);
ContentUris.appendId(builder, dayend);
Cursor eventCursor = contentResolver
.query(builder.build(), new String[] { "event_id", "begin",
"end", "description" }, "Calendars._id="+1, null,
"startDay ASC, startMinute ASC");
while (eventCursor.moveToNext()) {
String eventid = eventCursor.getString(0);
Date begin = new Date(eventCursor.getLong(1));
long eventtime = begin.getTime();
long now = new Date().getTime();
Date end = new Date(eventCursor.getLong(2));
String description = eventCursor.getString(3);
EventInfo info = new EventInfo(eventid, begin, end, description,
eventtime < now);
itemlist.add(info);
}
EventInfo是自己写的一个类,用来构造Event实例的,这就不贴了,详细的看项目代码
itemlist是这样
ArrayList<EventInfo> itemlist = new ArrayList<EventInfo>();
这段代码是查询哪个事件段的事件的,daystart是开始时间,dayend是结束时间,mYear是年,mMonth是月,mDay是日,也就是说当点击日历里的哪一天时,这三个作出相应的改变,就可以查询到用户点击的那一天的事件了。
在编辑事件界面,那个滑轮控件是仿iphone的,我这也给个项目,我自己也是照这个写的,都是自定义的一些View的应用。
插入事件到日历的Contentprovider里:
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null,
null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
这个方法可以得到calendar的uri,向Event表里插数据的代码如下:
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", starttime); // event starts at 11 minutes from now
values.put("dtend", endtime); // ends 60 minutes from now
String desc = contentedit.getText().toString();
values.put("description", desc);
values.put("visibility", 0);
values.put("hasAlarm", 1);
if (!TextUtils.isEmpty(mRule)) {
values.put("rrule", mRule);
}
Uri event = cr.insert(EVENTS_URI, values);
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", remindminute);
cr.insert(REMINDERS_URI, values);
这里最难的是添加事件的周期,我参考源码写的
private void setRepeatRule(int count) {
clearRecurrence();
Calendar cal = Calendar.getInstance();
switch (count) {
case 1:
mEventRecurrence.freq = EventRecurrence.DAILY;
mEventRecurrence.wkst = EventRecurrence
.calendarDay2Day(mFirstDayOfWeek);
mRule = mEventRecurrence.toString();
break;
case 2:
mEventRecurrence.freq = EventRecurrence.WEEKLY;
int[] days = new int[1];
int dayCount = 1;
int[] dayNum = new int[dayCount];
cal.clear();
cal.setTimeInMillis(starttime);
int weekday=cal.get(Calendar.DAY_OF_WEEK)-1;
days[0] = EventRecurrence.timeDay2Day(weekday);
// not sure why this needs to be zero, but set it for now.
dayNum[0] = 0;
mEventRecurrence.byday = days;
mEventRecurrence.bydayNum = dayNum;
mEventRecurrence.bydayCount = dayCount;
mEventRecurrence.wkst = EventRecurrence
.calendarDay2Day(mFirstDayOfWeek);
mRule = mEventRecurrence.toString();
break;
case 3:
mEventRecurrence.freq = EventRecurrence.WEEKLY;
int Count = 5;
int[] byday = new int[Count];
int[] bydayNum = new int[Count];
byday[0] = EventRecurrence.MO;
byday[1] = EventRecurrence.TU;
byday[2] = EventRecurrence.WE;
byday[3] = EventRecurrence.TH;
byday[4] = EventRecurrence.FR;
for (int day = 0; day < Count; day++) {
bydayNum[day] = 0;
}
mEventRecurrence.byday = byday;
mEventRecurrence.bydayNum = bydayNum;
mEventRecurrence.bydayCount = Count;
mEventRecurrence.wkst = EventRecurrence.calendarDay2Day(mFirstDayOfWeek);
mRule = mEventRecurrence.toString();
break;
case 4:
mEventRecurrence.freq = EventRecurrence.MONTHLY;
mEventRecurrence.bydayCount = 0;
mEventRecurrence.bymonthdayCount = 1;
int[] bymonthday = new int[1];
cal.clear();
cal.setTimeInMillis(starttime);
bymonthday[0] =cal.get(Calendar.DAY_OF_MONTH);
mEventRecurrence.bymonthday = bymonthday;
mEventRecurrence.wkst = EventRecurrence
.calendarDay2Day(mFirstDayOfWeek);
mRule = mEventRecurrence.toString();
break;
}
}
当点击不同的RadioButton,传不同的值。
EventRecurrence mEventRecurrence = new EventRecurrence();
这个EventRecurrence是从源码copy出来的,这里就不贴了。对日历的操作就差不多是这些了,里面的自定义View以后再写了。