2012-10-27 48 views
1

我一直在編寫一種鬧鐘應用程序,暫時只是爲了自己,但如果任何人在完成時想要它,我可以免費提供。無論如何,我一直受阻。我想從日曆中獲得的所有內容都是用戶日曆中的下一個事件,因此我可以顯示標題,時間,時間和地點。如何選擇使用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文檔。

回答

0

首先使用CalendarContract.Calendars中的常量查找所需的日曆。在NAME或CALENDAR_DISPLAY_NAME上進行查詢。從返回的行中,獲取_ID值。使用此值查詢CalendarContract.Events.CALENDAR_ID。這將爲您提供由CalendarContract.Events.CALENDAR_ID標識的日曆的所有事件。

你在做什麼是抓住所有事件所有日曆。

+0

哦,謝謝。這就說得通了。其實它可能是最好的方式。讓我不必爲用戶選擇一種他們想要的方式。你知道我能做些什麼來最快發生事件嗎? – NathanTempelman

+0

嘿@Joe 我這樣做是爲了得到最近的約會: 「,而(mCursor.moveToNext()){ \t \t \t \t如果(now.getTimeInMillis() NathanTempelman

相關問題