我一直在编写一种闹钟应用程序,暂时只是为了自己,但如果任何人在完成时想要它,我可以免费提供。无论如何,我一直受阻。我想从日历中获得的所有内容都是用户日历中的下一个事件,因此我可以显示标题,时间,时间和地点。如何选择使用android 4.0日历提供程序api从哪个日历中提取事件?
这里是我到目前为止有:
//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文档。
哦,谢谢。这就说得通了。其实它可能是最好的方式。让我不必为用户选择一种他们想要的方式。你知道我能做些什么来最快发生事件吗? – NathanTempelman
嘿@Joe 我这样做是为了得到最近的约会: “,而(mCursor.moveToNext()){ \t \t \t \t如果(now.getTimeInMillis()
NathanTempelman