save Calendar Event - Android Android OS

Android examples for Android OS:Calendar Event

Description

save Calendar Event

Demo Code


import android.annotation.SuppressLint;
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main{
    public static boolean saveCalendarEvent(Context context,
            CalendarEvent calendarEvent) {
        boolean operation = false;

        if (calendarEvent != null) {

            Uri event = context.getContentResolver()
                    .insert(getEventsContentUri(),
                            calendarEvent.getContentValues());

            if (event != null) {

                // get the event ID that is the last element in the Uri
                long eventID = Long.parseLong(event.getLastPathSegment());

                for (CalendarReminder reminder : calendarEvent.reminders) {
                    context.getContentResolver().insert(
                            getRemindersContentUri(),
                            reminder.getContentValues());
                }/*from w  ww .  j av  a 2 s  .  c om*/
                operation = true;
            }
        }

        return operation;
    }
    @SuppressLint("NewApi")
    private static Uri getEventsContentUri() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return CalendarContract.Events.CONTENT_URI;
        } else {
            return Uri.parse(getContentUri() + "/events");
        }
    }
    @SuppressLint("NewApi")
    private static Uri getRemindersContentUri() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return CalendarContract.Reminders.CONTENT_URI;
        } else {
            return Uri.parse(getContentUri() + "/reminders");
        }
    }
    @SuppressLint("NewApi")
    private static String getContentUri() {
        String AUTHORITY = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            AUTHORITY = "com.android.calendar";
        } else {
            AUTHORITY = "calendar";
        }
        return "content://" + AUTHORITY;
    }
}

Related Tutorials