push Appointments To Calendar - Android Android OS

Android examples for Android OS:Calendar Contract

Description

push Appointments To Calendar

Demo Code


//package com.java2s;

import java.util.TimeZone;

import android.app.Activity;
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.provider.CalendarContract.Events;

public class Main {
    public static long pushAppointmentsToCalendar(Activity curActivity,
            String title, String comment, Long eventIDFM, long startDate,
            long endDate, boolean needReminder, boolean needMailService,
            Boolean modify) {//from   w ww. j a v  a  2 s.  co  m

        /***************** Event: note(without alert) *******************/

        String eventUriString = "content://com.android.calendar/events";
        ContentValues eventValues = new ContentValues();

        eventValues.put("calendar_id", 1); // id, We need to choose from
        // our mobile for primary
        // its 1
        eventValues.put("title", title);

        if (modify && eventIDFM != -1) {
            deleteEventWithID(curActivity, eventIDFM);
        }
        eventValues.put("description", comment
                + " #ResistenciarteAPP #CodigoCiudadano");
        eventValues.put("eventLocation", "Resistencia");
        eventValues.put("dtstart", startDate);
        eventValues.put("dtend", endDate);

        // values.put("allDay", 1); //If it is bithday alarm or such
        // kind (which should remind me for whole day) 0 for false, 1
        // for true
        eventValues.put("eventStatus", 1); // This information is
        // sufficient for most
        // entries tentative (0),
        // confirmed (1) or canceled
        // (2):
        eventValues.put(Events.EVENT_TIMEZONE, TimeZone.getDefault()
                .getID());

        eventValues.put("hasAlarm", 1); // 0 for false, 1 for true

        Uri eventUri = curActivity.getApplicationContext()
                .getContentResolver()
                .insert(Uri.parse(eventUriString), eventValues);

        long eventID = Long.parseLong(eventUri.getLastPathSegment());

        if (needReminder) {
            /***************** Event: Reminder(with alert) Adding reminder to event *******************/

            String reminderUriString = "content://com.android.calendar/reminders";

            ContentValues reminderValues = new ContentValues();

            reminderValues.put("event_id", eventID);
            reminderValues.put("minutes", 45); // Default value of the
            // system. Minutes is a
            // integer
            reminderValues.put("method", 1); // Alert Methods: Default(0),
            // Alert(1), Email(2),
            // SMS(3)

            Uri reminderUri = curActivity.getApplicationContext()
                    .getContentResolver()
                    .insert(Uri.parse(reminderUriString), reminderValues);

        }


        return eventID;
    }

    public static void deleteEventWithID(Context curActivity, Long id) {

        ContentResolver cr = curActivity.getContentResolver();
        Uri EVENTS_URI = Uri.parse("content://com.android.calendar/events");
        Cursor cursor = cr.query(EVENTS_URI, new String[] { "calendar_id",
                "title", "description", "dtstart", "dtend",
                "eventLocation", "deleted", "_id" }, null, null, null);
        cursor.moveToFirst();
        // fetching calendars name
        String CNames[] = new String[cursor.getCount()];

        // fetching calendars id
        int cont = 0;

        for (int i = 0; i < CNames.length; i++) {
            String titleL = cursor.getString(1);
            long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
            if (id == (eventId)) {
                cr.delete(ContentUris.withAppendedId(EVENTS_URI, eventId),
                        null, null);
                break;
            }

            CNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }
    }
}

Related Tutorials