Retrieve the default calendar id in Android

前端 未结 5 684
走了就别回头了
走了就别回头了 2021-01-04 02:51

I have the following code for adding event to calendar.

The problem is that I don\'t know how to retrieve the default calendar id.

l         


        
5条回答
  •  渐次进展
    2021-01-04 03:13

    To get the list of calendars, you need to query the ContentResolver like this:

    public MyCalendar [] getCalendar(Context c) {
    
        String projection[] = {"_id", "calendar_displayName"};
        Uri calendars;
        calendars = Uri.parse("content://com.android.calendar/calendars");
    
        ContentResolver contentResolver = c.getContentResolver();
        Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);
    
        if (managedCursor.moveToFirst()){
            m_calendars = new MyCalendar[managedCursor.getCount()];
            String calName;
            String calID;
            int cont= 0;
            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do {
                calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                m_calendars[cont] = new MyCalendar(calName, calID);
                cont++;
            } while(managedCursor.moveToNext());
            managedCursor.close();
        }
        return m_calendars;
    
    }
    

提交回复
热议问题