[英]How do I go about selecting which calendar to pull events from using the android 4.0 calendar provider 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
它沒有顯示我的下一個約會,而是顯示了顯然在2月舉行的艾伯塔省家庭日。 我瀏覽了一下日歷,似乎是隨意選擇了Google的“ Canadian Holidays”日歷,因此Google確實將其添加到了手機中,而不是我實際放入的日歷中。我似乎無法弄清楚如何選擇正確的日歷,而不需要讓用戶每次都選擇它。
有人知道我該如何從我想要的日歷中獲得我想要的值嗎? 我真的很感激。
附言:我知道此應用程序無法在4.0之前的設備上運行,但我不太在意再花4個小時來嘗試破譯可怕的Google API文檔。
首先使用CalendarContract.Calendars中的常量找到所需的日歷。 在NAME或CALENDAR_DISPLAY_NAME上查詢。 從返回的行中獲得_ID值。 使用此值查詢CalendarContract.Events.CALENDAR_ID。 這將為您提供CalendarContract.Events.CALENDAR_ID標識的日歷的所有事件。
您正在做的是獲取所有日歷的所有事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.