简体   繁体   中英

How to get device Calendar (default calendar) events and display in our Application?

I am bit new to calendar. Can anybody suggest me how to get events (eg. birthday/meetings reminder) from default calendar (not google calendar or any other only default calendar) and display it in listview / recyclerview in our application?

This SO question describes reading and writing to the calendar. The key information you need to read the calendar(s) is:

Uri uri = CalendarContract.Calendars.CONTENT_URI;
String[] projection = new String[] {
   CalendarContract.Calendars._ID,
   CalendarContract.Calendars.ACCOUNT_NAME,
   CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
   CalendarContract.Calendars.NAME,
   CalendarContract.Calendars.CALENDAR_COLOR
};

Cursor calendarCursor = managedQuery(uri, projection, null, null, null);

Then you need to extract the data from the cursor:

ArrayList<String> ids = new ArrayList<String>();
try {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String id= cursor.getString(0);
                calendars.add(id);  
            }
        }
    } catch (AssertionError ex) { /*TODO: log exception and bail*/ }

Then you have the ids and need to query each one for events. This is described in the accepted answer to this SO question . Also this github repository has a solution. Most of these approaches only work on versions of Android greater than 4.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM