2012-11-05 58 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

相关问题