insert Calendar Event - Android Android OS

Android examples for Android OS:Calendar Event

Description

insert Calendar Event

Demo Code


//package com.java2s;
import java.util.Calendar;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;

import android.widget.Toast;
import android.content.Context;

public class Main {
    private static String calId = "";
    private static String calanderURL = "";
    private static String calanderEventURL = "";
    private static String calanderRemiderURL = "";
    private static Cursor userCursor;

    public static void insertEvent(Context context, String title,
            String description, int startHour, int endHour, int hasAlarm,
            int alarmTime) {

        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
            calanderURL = "content://com.android.calendar/calendars";
            calanderEventURL = "content://com.android.calendar/events";
            calanderRemiderURL = "content://com.android.calendar/reminders";

        } else {//from   w  w w .ja v  a 2  s .c  o m
            calanderURL = "content://calendar/calendars";
            calanderEventURL = "content://calendar/events";
            calanderRemiderURL = "content://calendar/reminders";
        }
        userCursor = context.getContentResolver().query(
                Uri.parse(calanderURL), null, null, null, null);
        if (userCursor.getCount() > 0) {
            userCursor.moveToFirst();
            calId = userCursor.getString(userCursor.getColumnIndex("_id"));

        }

        if (!calId.equals("")) {
            ContentValues event = new ContentValues();
            event.put("title", title);
            event.put("description", description);
            event.put("calendar_id", calId);

            Calendar mCalendar = Calendar.getInstance();
            mCalendar.set(Calendar.HOUR_OF_DAY, startHour);
            long start = mCalendar.getTime().getTime();
            mCalendar.set(Calendar.HOUR_OF_DAY, endHour);
            long end = mCalendar.getTime().getTime();

            event.put("dtstart", start);
            event.put("dtend", end);
            event.put("hasAlarm", hasAlarm);

            Uri newEvent = context.getContentResolver().insert(
                    Uri.parse(calanderEventURL), event);
            long id = Long.parseLong(newEvent.getLastPathSegment());
            ContentValues values = new ContentValues();
            values.put("event_id", id);

            values.put("minutes", alarmTime);
            context.getContentResolver().insert(
                    Uri.parse(calanderRemiderURL), values);
            Toast.makeText(context, "", Toast.LENGTH_LONG)
                    .show();
        } else {
            Toast.makeText(context,
                    "GMAIL GOOGLE",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

Related Tutorials