我一直在编写一种警报应用程序,暂时只是为了我自己,但如果有人在完成后想要它,我可能会免费提供它。无论如何,我一直受阻。我想要从日历中获取的只是用户日历中的下一个事件,这样我就可以显示标题、时间、截止时间和位置。
这是我到目前为止所得到的:
//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
它没有显示我的下一次约会,而是显示了艾伯塔省家庭日,这显然发生在 2 月。我用我的日历环顾四周,似乎它随意选择了我的“加拿大假期”日历,谷歌非常有帮助地添加到我的手机中,而不是我实际放入的日历。我似乎无法弄清楚如何选择正确的日历,而无需让用户每次都选择它。
有人知道我将如何从我想要的日历中获得我想要的值吗?我真的很感激。
PS 我知道这个应用程序不能在 4.0 之前的设备上运行,但我不在乎再花 4 个小时来尝试破译可怕的 google api 文档。