add Event To Calendar - Android Android OS

Android examples for Android OS:Calendar Event

Description

add Event To Calendar

Demo Code


//package com.java2s;

import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

public class Main {
    public static final String TAG = "CalendarUtility";

    @SuppressWarnings("deprecation")
    public static void addToCalendar(Context ctx, final String title,
            final long dtstart, final long dtend, final String desc) {
        final ContentResolver cr = ctx.getContentResolver();
        Cursor cursor;//from  w  ww  .j av  a  2s . c  om
        if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            cursor = cr
                    .query(Uri
                            .parse("content://com.android.calendar/calendars"),
                            new String[] { "_id", "displayname" }, null,
                            null, null);
        else
            cursor = cr
                    .query(Uri.parse("content://calendar/calendars"),
                            new String[] { "_id", "displayname" }, null,
                            null, null);
        if (cursor.moveToFirst()) {
            final String[] calNames = new String[cursor.getCount()];
            final int[] calIds = new int[cursor.getCount()];
            for (int i = 0; i < calNames.length; i++) {
                calIds[i] = cursor.getInt(0);
                calNames[i] = cursor.getString(1);
                cursor.moveToNext();
            }

            AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
            builder.setSingleChoiceItems(calNames, -1,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {

                            Log.d(TAG, "build");

                            ContentValues cv = new ContentValues();
                            cv.put("calendar_id", calIds[which]);
                            cv.put("title", title);
                            cv.put("dtstart", dtstart);
                            cv.put("hasAlarm", 1);
                            cv.put("description", desc);
                            cv.put("dtend", dtend);

                            Uri newEvent;
                            if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
                                newEvent = cr.insert(
                                        Uri.parse("content://com.android.calendar/events"),
                                        cv);
                                Log.d(TAG, ">=8" + newEvent.toString());
                            } else {
                                newEvent = cr.insert(
                                        Uri.parse("content://calendar/events"),
                                        cv);
                                Log.d(TAG, "<8");
                            }

                            if (newEvent != null) {
                                long id = Long.parseLong(newEvent
                                        .getLastPathSegment());
                                ContentValues values = new ContentValues();
                                values.put("event_id", id);
                                values.put("method", 1);
                                values.put("minutes", 15); // 15 minutes
                                if (Integer.parseInt(Build.VERSION.SDK) >= 8)
                                    cr.insert(
                                            Uri.parse("content://com.android.calendar/reminders"),
                                            values);
                                else
                                    cr.insert(
                                            Uri.parse("content://calendar/reminders"),
                                            values);

                            }
                            dialog.cancel();
                        }

                    });

            builder.create().show();
        }
        cursor.close();
    }
}

Related Tutorials