Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Calendar and events in Android 2.2

What I want: I want to add calendar events in Android 2.2.

What I Have: I have added an event using the below code

    Uri calendars = Uri.parse("content://com.android.calendar/events");
    Cursor managedCursor = managedQuery(calendars, null, null, null, null);

    startManagingCursor(managedCursor);
    managedCursor.moveToFirst();

    String ID = null;

    do
    {
        ID = managedCursor.getString(managedCursor.getColumnIndexOrThrow("_id"));
    } 
    while (managedCursor.moveToNext());
    managedCursor.close();      

    int NewID = Integer.parseInt(ID) + 1;

    ContentValues event = new ContentValues();
    event.put("calendar_id", NewID);  // --- Some confusion Here with the ID,  
                                      // --- not sure how to use it here
    event.put("title", "New Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Somewhere");

    long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
    long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;

    event.put("dtstart", startTime);
    event.put("dtend", endTime);

    event.put("allDay", 0); // 0 for false, 1 for true
    event.put("eventStatus", 1);
    event.put("visibility", 0);
    event.put("transparency", 0);
    event.put("hasAlarm", 0); // 0 for false, 1 for true

    Uri eventsUri = Uri.parse("content://com.android.calendar/events");
    Uri insertedUri = getContentResolver().insert(eventsUri, event);

What is the problem:
So far I have been successful in adding a single event on the specified date time, and apparently NewID's role is suspicious to me. When I try to add some other event, I get the returned Uri insertedUri and it shows me the newly added ID at the end of the URI. But I cant see any such event on the device. May be there is some problem in my understanding of the Calendar and events, or differences in both and their ID's. Kindly guide me what I am missing or doing wrong.

Regards,
Khawar

like image 816
Khawar Avatar asked Jun 01 '11 06:06

Khawar


People also ask

How do I add a calendar to my android?

Go to calendar.google.com. In the menu on the left, click on the + icon next to Other calendars. You can create a new calendar or subscribe to an existing calendar using that person's account.

How do I add an event from Gmail to my Android calendar?

If you are already logged in to Google Apps, open Google Mail by clicking the [Mail] tab at the top of the screen. Open the email you wish to turn into an appointment. From across the top of the screen, click [More]. From the drop-down menu, click “Create event.” You will be taken to a Google Calendar edit event page.


1 Answers

Most of the code is fine, you just need to know a little bit of concepts regarding calendar. Actually there is more than one calendar types in Android.

To Traverse all the calendars, Use following Uri for 2.2:

Uri calendars = Uri.parse("content://com.android.calendar"+ "/calendars");

And get the values of 'id' and 'name', you will get the idea.

NewID's role is suspicious to me

Your insertion code is fine, you just need to give the id of that calendar in which you want to insert any event.

I still believe that even myself needs to learn alot so if you have anything to tell or correct, you are most welcome. Following links helped me:

Working with Android Calendars

Accessing Calendars events without using gdata api

like image 119
Farhan Avatar answered Nov 15 '22 10:11

Farhan