get All Calendars - Android Android OS

Android examples for Android OS:Calendar Contract

Description

get All Calendars

Demo Code


import android.content.Context;
import android.database.Cursor;
import android.provider.CalendarContract;
import android.util.Log;
import android.util.Pair;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

public class Main{
    public static List<Pair<String, Boolean>> getAllCalendars(
            Context context) {//from   w w w  .ja v a2  s  .  com
        // Only return calendars that are marked as synced to device.
        // (This is different from the display flag)
        List<Pair<String, Boolean>> calendars = new ArrayList<Pair<String, Boolean>>();

        try {
            Cursor cursor = context.getContentResolver().query(
                    CalendarContract.Calendars.CONTENT_URI,
                    CalendarsQuery.PROJECTION,
                    CalendarContract.Calendars.SYNC_EVENTS + "=1", null,
                    null);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    calendars.add(new Pair<String, Boolean>(cursor
                            .getString(CalendarsQuery.ID), cursor
                            .getInt(CalendarsQuery.VISIBLE) == 1));

                }

                cursor.close();
            }

        } catch (SecurityException e) {
            Log.v("launcher_calendar", e.getMessage());
            return null;
        }

        return calendars;
    }
}

Related Tutorials