我一直在编写一种报警应用程序,暂时只针对我自己,但如果有人想要的话,我可以免费提供它。无论如何,我一直受到阻碍。我想要的日历是用户日历中的下一个事件,因此我可以显示标题,时间,时间和位置。
这是我到目前为止所得到的:
//get cursor to first row of table of events
mCursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
mCursor.moveToFirst();
//variables; title, startdate in miliseconds, etc
String nextAppointmentTitle = "N/A";
Long start = 0L; //tf(start) for appointmentTime
String timeUntilNextAppointment = "N/A";
String nextAppointmentLocation = "N/A";
Format df = DateFormat.getDateFormat(this);
Format tf = DateFormat.getTimeFormat(this);
try {
nextAppointmentTitle = mCursor.getString(0);
start = mCursor.getLong(1);
} catch (Exception e) {
//ignore for the time being
}
//create a date object for the time of the event
GregorianCalendar appointmentTime = (GregorianCalendar) GregorianCalendar.getInstance();
appointmentTime.setTimeInMillis(start);
//create date object for current time
GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance();
//get the time from current time until start of event
long millisUntilNextMeeting = (long) appointmentTime.getTimeInMillis() - (long) now.getTimeInMillis();
GregorianCalendar timeUntilNextMeeting = (GregorianCalendar) GregorianCalendar.getInstance();
timeUntilNextMeeting.clear();
timeUntilNextMeeting.setTime(new Date(millisUntilNextMeeting));
millisUntilNextMeeting= (millisUntilNextMeeting/1000) /60;
if (timeUntilNextMeeting.get(GregorianCalendar.HOUR_OF_DAY) > 0) {
int hours = timeUntilNextMeeting.get(GregorianCalendar.HOUR_OF_DAY) + 6;
timeUntilNextAppointment = "" + hours + " hours";
} else {
timeUntilNextAppointment = "" + timeUntilNextMeeting.get(GregorianCalendar.MINUTE) + " minutes";
}
// ... do things with values
而不是展示我的下一个约会,它显示的是艾伯塔省的家庭日,显然是在二月份。我做了一些用我的日历环顾四周,似乎它已经随意选择了我的“加拿大假期”日历,谷歌如此有用地添加到我的手机,而不是我实际放入的那个。我似乎无法弄清楚如何选择正确的日历而不必让用户每次都选择它。
有人知道如何从我想要的日历中获得我想要的值吗?我真的很感激一只手。
PS我知道这个应用程序不适用于4.0之前的设备,但是我并不在意另外4小时试图破解可怕的谷歌api文档。
答案 0 :(得分:0)
首先使用CalendarContract.Calendars中的常量查找所需的日历。查询NAME或CALENDAR_DISPLAY_NAME。从返回的行中获取_ID值。使用此值可以查询CalendarContract.Events.CALENDAR_ID。这将为您提供CalendarContract.Events.CALENDAR_ID标识的日历的所有事件。
您正在做的是抓取所有日历的所有活动。