2012-11-05 86 views
6

嗨,我能夠使用這個代碼如何從日曆中獲取所有事件?

cur = null; 
    cr = getContentResolver(); 


    // Construct the query with the desired date range. 
    Uri.Builder builder = Instances.CONTENT_URI.buildUpon(); 

    ContentUris.appendId(builder, 0); 
    ContentUris.appendId(builder, endMillis); 


    // Submit the query 
    cur = cr.query(builder.build(), 
     INSTANCE_PROJECTION, 
     null, 
     null, 
     null); 

但正如你看到的,我可以給定的持續時間內只得到事件,但我需要的所有事件(不排除重複事件的任何時間規範)是讓事件這可能嗎?如何 ? 請幫助我。

cur = cr.query(Uri.parse("content://com.android.calendar/events"), 
     INSTANCE_PROJECTION, 
     null, 
     null, 
     null); 

它給出了錯誤java.lang.IllegalArgumentException異常:

+0

是什麼實例? – njzk2

回答

10

試試這個代碼...

public class Utility { 

    public static ArrayList<String> nameOfEvent = new ArrayList<String>(); 
    public static ArrayList<String> startDates = new ArrayList<String>(); 
    public static ArrayList<String> endDates = new ArrayList<String>(); 
    public static ArrayList<String> descriptions = new ArrayList<String>(); 

    public static ArrayList<String> readCalendarEvent(Context context) { 
     Cursor cursor = context.getContentResolver() 
       .query(
         Uri.parse("content://com.android.calendar/events"), 
         new String[] { "calendar_id", "title", "description", 
           "dtstart", "dtend", "eventLocation" }, null, 
         null, null); 
     cursor.moveToFirst(); 
     // fetching calendars name 
     String CNames[] = new String[cursor.getCount()]; 

     // fetching calendars id 
     nameOfEvent.clear(); 
     startDates.clear(); 
     endDates.clear(); 
     descriptions.clear(); 
     for (int i = 0; i < CNames.length; i++) { 

      nameOfEvent.add(cursor.getString(1)); 
      startDates.add(getDate(Long.parseLong(cursor.getString(3)))); 
      endDates.add(getDate(Long.parseLong(cursor.getString(4)))); 
      descriptions.add(cursor.getString(2)); 
      CNames[i] = cursor.getString(1); 
      cursor.moveToNext(); 

     } 
     return nameOfEvent; 
    } 

    public static String getDate(long milliSeconds) { 
     SimpleDateFormat formatter = new SimpleDateFormat(
       "dd/MM/yyyy hh:mm:ss a"); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(milliSeconds); 
     return formatter.format(calendar.getTime()); 
    } 
} 

您還可以檢查此

Getting events from calendar

+0

謝謝阿米特,但不工作看到我編輯的問題...... –

+0

http://stackoverflow.com/questions/4302209/android-calendar-events 看看這個 –

+0

如何獲得Listofattendees使用日曆 – Adevelopment

相關問題