I’ve been coding a sort of alarm app, just for myself for the time being, but I might put it up for free if anybody wants it when it’s done. In any case, I’ve been stymied. All I want from the calendar is the next event in the user’s calendar so I can display the title, time, time until, and location.
Here’s what I’ve got so far:
//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
Rather than showing my next appointment, it’s showing Alberta Family Day, which apparently takes place in February. I did some looking around with my calendars and it appears that it has arbitrarily selected my “Canadian Holidays” calendar that Google so helpfully added to my phone, instead of the one I actually put things in. I can’t seem to figure out how to select the correct calendar without resorting to having the user select it every time.
Does anybody know offhand how I would get the values I want from the calendar I want? I’d really appreciate a hand.
PS I know that this app won’t work on pre 4.0 devices, but I don’t care enough to throw another 4 hours at trying to decipher the horrible google api docs.
First find the calendar you want by using constants in CalendarContract.Calendars. Query on either NAME or CALENDAR_DISPLAY_NAME. From the returned row, get the _ID value. Use this value to query CalendarContract.Events.CALENDAR_ID. That will give you all the events for the calendar identified by CalendarContract.Events.CALENDAR_ID.
What you were doing is grabbing all the events for all calendars.