Create and insert new calendar into android database - Android Database

Android examples for Database:ContentValues

Description

Create and insert new calendar into android database

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{
    /** The main/basic URI for the android calendars table */
    private static final Uri CAL_URI = CalendarContract.Calendars.CONTENT_URI;
    static final String LOG = "CalendarUtil";
    /**/*w  ww . j av a  2s . c  o  m*/
     * Create and insert new calendar into android database
     * 
     * @param ctx
     *            The context (e.g. activity)
     */
    public static String createCalendar(CalendarRequest crx)
            throws Exception {
        ContentResolver cr = crx.getContext().getContentResolver();
        final ContentValues cv = getCalendarContentValues(
                crx.getCalendarName(), crx.getAccountName());
        Uri calUri = buildCalUri(crx.getAccountName());
        // insert the calendar into the database

        Uri u = cr.insert(calUri, cv);
        String s = u.toString();
        int index = s.indexOf("calendars/");
        int index2 = s.indexOf("?");
        if (index > -1) {
            index += 10;
            String id = s.substring(index, index2);
            Log.i(LOG, "----------- Substring gave this calendar id: " + id);
        }

        Log.i(LOG, "Calendar created: " + u.toString());

        return u.toString();
    }
    private static ContentValues getCalendarContentValues(
            String calendarName, String accountName) {

        final ContentValues cv = new ContentValues();
        cv.put(Calendars.ACCOUNT_NAME, accountName);
        cv.put(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
        cv.put(Calendars.NAME, calendarName);
        cv.put(Calendars.CALENDAR_DISPLAY_NAME, calendarName);
        cv.put(Calendars.CALENDAR_COLOR, 0xEA8561);
        // user can only read the calendar
        cv.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        cv.put(Calendars.OWNER_ACCOUNT, accountName);
        cv.put(Calendars.VISIBLE, 1);
        cv.put(Calendars.SYNC_EVENTS, 1);
        return cv;
    }
    /** Builds the Uri for your Calendar in android database (as a Sync Adapter) */

    private static Uri buildCalUri(String accountName) {
        return CAL_URI
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, accountName)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();
    }
}

Related Tutorials