Calendar Events

Monday, 24 September 2012 4 comments
As Android is getting strong day by day, Its keep on changing its APIs to let the developers do their development in a effective way. In this row, Its changed the Calendar API to use as Public  URI or can use Intents to let the user to enter the needed information. Will show you these two methods with clear example.


Doing with Intents : 


The above set of code will trigger the intent to open the device calendar application which in turn open the create event screen. User can enter the needed information and thus can create an event. To make it more user friendly and if you know what user will enter to create the event, You can provide the Create Event screen with the pre-filled data. You can do like

The above set of code will populate the Create event screen with the title "Bleed in Android" and with all the details given above.

Programmatically adding Events to Calendar : 


As i said in the beginning of this tutorial, android has changed its API as Android version grows. So we have to take that in account. The appropriate API can be obtained as 

By doing so, we will get the required data about all the available calendars in that device. All the data will be stored in a cursor. Let choose the needed calendar and its data.

This will get the calendar names and their IDs. Lets choose the one we need.

Adding Events to calendar

Add the Events without user interaction.

If you need to update the event which you created already, just follow all the above steps except the inert query. It can be written as, 


This will update the calendar event which matches the Calendar id and the Title which is unique in my case. Well if you need to delete the inserted or updated record, do something like this.

We can also add reminder events to the added o updated Calendar events. The way to do is,


This will add the remainder which will alert you before a day. The remainder values is taken in minutes. So added the minutes of a day as remainder.  Close the Cursor after all your transactions.  Kindly don't try this in Emulator, Since it don't have the Calendar Application, it may crash the Application if not properly handled. Happy coding. Let me know if any queries. 


  







4 comments:

  • Unknown said...

    E/AndroidRuntime(19326): android.database.sqlite.SQLiteException: expected 1 row from this query but query returned no data. check the query: SELECT canPartiallyUpdate FROM view_events WHERE _id = ?

Post a Comment

 

©Copyright 2011 Bleed in Android | TNB