从CalendarContract获取与会者详细信息

时间:2013-12-02 05:52:47

标签: android android-contentprovider android-calendar

我正在尝试创建一个应用,该应用会从设备上的日历应用中获取EventsAttendees的详细信息。

我面临的问题是:

1)。在很多活动中,Title和他们的参加者都不匹配。

2)。在许多活动中,我有0名与会者(主要是为即将举行的活动)。

这是我的代码:(请让我知道错误)。

public class ReadCalendar {
static Cursor cursor;

public static void readCalendar(Context context) {

ContentResolver contentResolver = context.getContentResolver();

// Fetch a list of all calendars synced with the device, their display names and whether the

cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
            (new String[] { Calendars._ID, Calendars.NAME}), null, null, null);

HashSet<String> calendarIds = new HashSet<String>();

try
{
    System.out.println("Count="+cursor.getCount());
    if(cursor.getCount() > 0)
    {
        System.out.println("the control is just inside of the cursor.count loop");
    while (cursor.moveToNext()) {

         String _id = cursor.getString(0);
         String displayName = cursor.getString(1);
         //Boolean selected = !cursor.getString(2).equals("0");

        System.out.println("Id: " + _id + " Display Name: " + displayName);
        calendarIds.add(_id);
    }
}
}
catch(AssertionError ex)
{
    ex.printStackTrace();
}
catch(Exception e)
{
    e.printStackTrace();
}


// For each calendar, display all the events from the previous week to the end of next week.        
for (String id : calendarIds) {
    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    //Uri.Builder builder = Uri.parse("content://com.android.calendar/calendars").buildUpon();
    long now = new Date().getTime();

    ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS * 10000);
    ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS * 10000);

    Log.e("123", "Calender ID---->>>>>>"+id);
Cursor eventCursor = contentResolver.query(builder.build(),
            new String[]  { Events.TITLE, "begin", "end", "allDay", Events._ID, Events.CALENDAR_ID}, Events.CALENDAR_ID+"=" + id,
            null, "_id ASC");

    Log.e("123","eventCursor count====="+eventCursor.getCount());
    if(eventCursor.getCount()>0)
    {

        if(eventCursor.moveToFirst())
        {
            do
            {
                Object mbeg_date,beg_date,beg_time,end_date,end_time;

                final String title = eventCursor.getString(0);
                final Date begin = new Date(eventCursor.getLong(1));
                final Date end = new Date(eventCursor.getLong(2));
                final Boolean allDay = !eventCursor.getString(3).equals("0");
                final String eventId = eventCursor.getString(4);
                final String calendarID = eventCursor.getString(5);

                Log.e("123", "Event Id----->>>>>"+eventId+"---------calendarId----->>>"+calendarID);

        /*  System.out.println("Title: " + title + " Begin: " + begin + " End: " + end +
                    " All Day: " + allDay);
        */  
                Log.e("123","Title:"+title);
                Log.e("123","Begin:"+begin);
                Log.e("123","End:"+end);
                Log.e("123","All Day:"+allDay);

             // Attendees Code
                Cursor eventAttendeesCoursor = contentResolver.query(CalendarContract.Attendees.CONTENT_URI, new String []{ Attendees.ATTENDEE_NAME, Attendees.EVENT_ID}, Attendees.EVENT_ID +" = " + eventId, null, null);
                Log.e("123", "Count of no of attendees-----"+eventAttendeesCoursor.getCount());
                if(eventAttendeesCoursor.getCount()>0)
                {

                    if(eventAttendeesCoursor.moveToFirst())
                    {
                        do {
//                              Log.e("123", "Attendees Name---->>>"+ eventAttendeesCoursor.getString(0));
                            Log.e("123", "Attendees Event ID---->>>"+ eventAttendeesCoursor.getString(1));
                        } while(eventAttendeesCoursor.moveToNext());
                    }
                }

            }
            while(eventCursor.moveToNext());
        }
    }
    break;
}
}
}

1 个答案:

答案 0 :(得分:2)

我没有详细查看您的代码,但我遇到了同样的问题 - 这是因为实例ID和事件ID之间的混淆。我确实看到你的uri(构建器)基于实例,但你需要的字段是event.id:这些是不同的。

重要的是,与会者表基于EVENT ID - 而不是实例ID - 因此它可能会解释您的问题。 一致地完成所有操作(即根据您的URI从INSTANCE表中提取EVENT id并将其传递给ATTENDEES表。看看是否有帮助。