query Calendars - Android Android OS

Android examples for Android OS:Calendar Contract

Description

query Calendars

Demo Code


import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Attendees;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Reminders;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class Main{
    public static final String[] EVENT_PROJECTION = new String[] {
            Calendars._ID, // 0
            Calendars.ACCOUNT_NAME, // 1
            Calendars.CALENDAR_DISPLAY_NAME, // 2
            Calendars.OWNER_ACCOUNT // 3
    };//from  w  w  w . j av  a 2s.  c o m
    private static final int PROJECTION_ID_INDEX = 0;
    private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
    private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;
    private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
    static final String LOG = "CalendarUtil";
    public static List<CalendarRequest> queryCalendars(Context ctx,
            String email) {
        List<CalendarRequest> list = new ArrayList<CalendarRequest>();
        Cursor cur = null;
        ContentResolver cr = ctx.getContentResolver();
        Uri uri = Calendars.CONTENT_URI;
        String selection = Calendars.ACCOUNT_NAME + " = '" + email + "'";
        //String[] selectionArgs = new String[] {email}; 
        // Submit the query and get a Cursor object back. 
        cur = cr.query(uri, EVENT_PROJECTION, selection, null, null);
        // Use the cursor to step through the returned records
        while (cur.moveToNext()) {
            long calID = 0;
            String displayName = null;
            String accountName = null;
            String ownerName = null;

            // Get the field values
            calID = cur.getLong(PROJECTION_ID_INDEX);
            displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
            accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
            ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
            Log.i(LOG, "Calendar found, displayName: " + displayName
                    + " acctName: " + accountName + " owner: " + ownerName
                    + " id: " + calID);
            CalendarRequest crr = new CalendarRequest();
            crr.setCalendarID(calID);
            crr.setCalendarName(displayName);
            list.add(crr);

        }
        return list;
    }
}

Related Tutorials