Create event - Add an event to our calendar - Android Android OS

Android examples for Android OS:Calendar Event

Description

Create event - Add an event to our calendar

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 events table */
    private static final Uri EVENT_URI = CalendarContract.Events.CONTENT_URI;
    static final String LOG = "CalendarUtil";
    /**//  w w w  . ja  va 2 s .com
     * Create event - Add an event to our calendar
     * 
     * @param dtstart
     *            Event start time (in millis)
     * @param dtend
     *            Event end time (in millis)
     */
    public static long addEvent(CalendarRequest crx) throws Exception {
        ContentResolver cr = crx.getContext().getContentResolver();
        ContentValues cv = new ContentValues();
        cv.put(Events.CALENDAR_ID, crx.getCalendarID());
        cv.put(Events.TITLE, crx.getTitle());
        cv.put(Events.DTSTART, crx.getDateStart());
        cv.put(Events.DTEND, crx.getDateEnd());
        cv.put(Events.EVENT_LOCATION,
                SharedUtil.getCompany(crx.getContext()).getCompanyName());
        cv.put(Events.DESCRIPTION, crx.getDescription());
        // TODO - get country, see if we can tease the time zone out of it, no?
        cv.put(Events.EVENT_TIMEZONE, "Africa/Johannesburg");
        Uri uri = buildEventUri(crx.getAccountName());
        Uri u = cr.insert(uri, cv);
        Log.w(LOG, "-- uri returned: " + u.toString());
        String id = null;
        String s = u.toString();
        int index = s.indexOf("events/");
        int index2 = s.indexOf("?");
        if (index > -1) {
            index += 7;
            id = s.substring(index, index2);
            Log.i(LOG, "----------- Substring gave this event id: " + id);
        }

        Log.i(LOG, "Event added: " + u.toString());
        long eventID = Long.parseLong(id);
        //check if attendees
        if (crx.getAttendeeRequestList() != null) {
            for (AttendeeRequest ar : crx.getAttendeeRequestList()) {
                ar.setEventID(eventID);
            }
            addAttendees(crx.getContext(), crx.getAttendeeRequestList());
        }
        return eventID;
    }
    /** Builds the Uri for events (as a Sync Adapter) */
    private static Uri buildEventUri(String accountName) {
        return EVENT_URI
                .buildUpon()
                .appendQueryParameter(
                        CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                .appendQueryParameter(Calendars.ACCOUNT_NAME, accountName)
                .appendQueryParameter(Calendars.ACCOUNT_TYPE,
                        CalendarContract.ACCOUNT_TYPE_LOCAL).build();
    }
    public static void addAttendees(Context ctx, List<AttendeeRequest> list)
            throws Exception {
        for (AttendeeRequest ar : list) {
            addAttendee(ctx, ar.getEventID(), ar.getEmail(),
                    ar.getAttendeeName());
        }
    }
    private static void addAttendee(Context ctx, long eventID,
            String email, String attendeeName) throws Exception {
        ContentResolver cr = ctx.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(Attendees.ATTENDEE_NAME, attendeeName);
        values.put(Attendees.ATTENDEE_EMAIL, email);
        values.put(Attendees.ATTENDEE_RELATIONSHIP,
                Attendees.RELATIONSHIP_ATTENDEE);
        values.put(Attendees.ATTENDEE_TYPE, Attendees.TYPE_REQUIRED);
        values.put(Attendees.ATTENDEE_STATUS,
                Attendees.ATTENDEE_STATUS_INVITED);
        values.put(Attendees.EVENT_ID, eventID);
        Uri u = cr.insert(Attendees.CONTENT_URI, values);
        Log.i(LOG,
                "attendee added ... " + attendeeName + "\n" + u.toString());
    }
}

Related Tutorials