-1

I am trying to implement finding and selecting events in calendar on Android Application. I believe that, find and select have completely different implementation.

I have scoured the web to no avail. I would appreciate a helping hand.

user788511
  • 1,726
  • 2
  • 30
  • 52
  • what is the meaning find and select – kannappan Aug 02 '11 at 09:44
  • I do not understand your question but i can try to elaborate. find event is essentially used to locate an event based on search parameters like start date, end date or description while select event is the basic selection query. – user788511 Aug 02 '11 at 09:47

2 Answers2

5

manifest

<uses-permission android:name="android.permission.READ_CALENDAR" />

These examples are for <= 2.1 version;

first; find out which calendars exist

Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id",  "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
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();
}
cursor.close();

Fetching all events, and particular event is done by specifying range
ContentResolver contentResolver = getContentResolver();

Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
        long now = new Date().getTime();
        ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
        ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);

and then let's say you wish to log events ID from calendar with ID = 1

Cursor eventCursor = contentResolver.query(builder.build(),
                new String[] { "event_id"}, "Calendars._id=" + 1,
                null, "startDay ASC, startMinute ASC"); 
        // For a full list of available columns see http://tinyurl.com/yfbg76w
        while (eventCursor.moveToNext()) {
            String uid2 = eventCursor.getString(0);
            Log.v("eventID : ", uid2);

        }

and for some source code check Jim Blackler's Accessing the internal calendar database inside Google Android applications and Android Calendar Events

Community
  • 1
  • 1
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

It is for 2.2+ version only

I used the code below to add event in calender

if (Build.VERSION.SDK_INT >= 14) {
            Calendar cal = Calendar.getInstance();
            Intent intent = new Intent(Intent.ACTION_INSERT)
                    .setData(Events.CONTENT_URI)
                    /*
                     * .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                     * beginTime.getTimeInMillis())
                     * .putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                     * endTime.getTimeInMillis())
                     */
                    .putExtra(Events.TITLE,
                            "A Test Event from android app by Neha ")
                    .

                    putExtra("beginTime", cal.getTimeInMillis())
                    .putExtra("allDay", true)
                    .putExtra("rrule", "FREQ=YEARLY")
                    .putExtra("endTime",
                            cal.getTimeInMillis() + 60 * 60 * 1000)
                    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
            startActivity(intent);
        }

        else {
            Calendar cal = Calendar.getInstance();
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra("beginTime", cal.getTimeInMillis());
            intent.putExtra("allDay", true);
            intent.putExtra("rrule", "FREQ=YEARLY");
            intent.putExtra("endTime",
                    cal.getTimeInMillis() + 60 * 60 * 1000);
            intent.putExtra("title",
                    "A Test Event from android app by Neha");
            startActivity(intent);
        }

and the code below i used to edit event

long calendarEventID = GetMaxID();
        Uri uri = Uri.parse("content://com.android.calendar/events/"
                + String.valueOf(calendarEventID));

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);

        intent.putExtra("beginTime", startDate);
        intent.putExtra("endTime", endDate);
        startActivity(intent);

and GetMaxId as

public long GetMaxID() {

    ContentResolver contentResolver = getContentResolver();

    Uri local_uri;
    local_uri = Uri.parse("content://com.android.calendar/events");

    Cursor cursor = contentResolver.query(local_uri, new String[] {
            "_id as max_id", "dtstart", "dtend" }, null, null, "_id");

    cursor.moveToFirst();
    long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));

    startDate = cursor.getLong(cursor.getColumnIndex("dtstart"));
    endDate = cursor.getLong(cursor.getColumnIndex("dtend"));

    return max_val + 1;

}

and put the permission in menifest as

<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>

Hope it would help to someone also.

Milan Shukla
  • 1,602
  • 18
  • 16