check if have Event in Calendar - Android Android OS

Android examples for Android OS:Calendar Event

Description

check if have Event in Calendar

Demo Code


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Color;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;
import android.text.format.Time;

public class Main{
    public static final String[] INSTANCES_PROJECTION = new String[] {
            Instances.EVENT_ID, Instances.TITLE, Instances.START_DAY };
    public static boolean haveEvent(final Context context,
            final String eventDate) {
        boolean haveEvents = false;
        if (context != null && eventDate != null && !eventDate.equals("")) {
            Cursor cursor = null;
            final String getDataMilliSeconds = DataProcess
                    .getDataMilliSeconds(eventDate
                            + DataProcess.DEFAULT_EVENT_TIME);
            ///*w w  w  .ja v  a 2  s  .  c o m*/
            try {
                cursor = getCursor(context, getDataMilliSeconds);
                if (cursor != null && cursor.getCount() > 0) {
                    haveEvents = true;
                }
            } catch (SQLException e) {
                System.out
                        .println("[CalendarIntentHelper][haveEvent]SQLException:"
                                + e);
            } catch (Exception ex) {
                System.out
                        .println("[CalendarIntentHelper][haveEvent]Exception:"
                                + ex);
            }
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return haveEvents;
    }
    private static Cursor getCursor(final Context context,
            final String getDataMilliSeconds) {
        Cursor cursor = null;
        String selection[] = null;
        final String where = INSTANCES_PROJECTION[INSTANCES_START_DAY_INDEX]
                + " = ?";
        //
        selection = new String[] { String.valueOf(Time.getJulianDay(
                Long.valueOf(getDataMilliSeconds), 0)) };
        cursor = context.getContentResolver().query(
                getInstancesUri(getDataMilliSeconds), INSTANCES_PROJECTION,
                where, selection, null);
        return cursor;
    }
    private static Uri getInstancesUri(final String getDataMilliSeconds) {
        final Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
        // StartDate
        ContentUris.appendId(builder, Long.valueOf(getDataMilliSeconds));
        // EndDate
        ContentUris.appendId(builder, Long.valueOf(getDataMilliSeconds));
        return builder.build();
    }
}

Related Tutorials