Having had a need to be able to create and delete calendar events I produced my first calendar library (most of the code I got from the net).
The functions are;
- this will return a 'Map' with the calendar Id and name. With this ID we can then create or delete events in this calendar.GetListOfAllCalendars(boolean)
- used to create a calendar event using the calendar ID found above. At the moment the fields allowed are Calendar ID, TITLE, DESCRIPTION, LOCATION, STARTTIME, ENDTIME, Recurring Rule and ALLDAY.CreateEvent(int CalID, String Title, String Desc, String Location, long StartTime, long EndTime, String RRule, boolean AllDay)
Added with V1.1
- used to create a calendar event using the calendar ID found above. At the moment the fields allowed are Calendar ID, TITLE, DESCRIPTION, LOCATION, STARTTIME, ENDTIME, Reminder Time, Recurring Rule and ALLDAY.CreateEventWithReminder(int CalID, String Title, String Desc, String Location, long StartTime, long EndTime, int ReminderTime, String RRule, boolean AllDay)
- using the calendar ID we can get a 'List' of all events for that calendar, items returned are the same as in the CreateEvent as well as the Event ID. the data is in the form of a List you just need to iterate thru it.GetListofAllEventsforCalendar(int)
- As above but you can specify start and finish dates/time in milliseconds from epoch.GetListofEventsforCalendarBetweenDates(int, long, long)
- similar to above but filter on 'string' in the Description field.ListofEventsWithDescKeywordBetweenDates(int, String, long, long)
- this time filter on Title fieldListofEventsWithTitleKeywordBetweenDates(int, String, long, long)
- once you have the event ID you can delete it with this routine.DeleteCalendarEntry(int)
This library works on the "unofficial" google model which works directly on the calendars on the phone its self this then syncs up to your google account online. The advantage is no problems with authorization but it probably will not work with all phones. I do hope to create a similar one using the new API which will work on everything and will not stop working. I have tried it on a HTC Desire 2.2 and an Asus tablet 3.1? and it worked on both.
To use it in your program,
B4X:
Dim Mycal As MyCalendar
Mycal.Initialize
myMap = Mycal.GetListOfAllCalendars(False)
Mycal.CreateEvent(1,"Test title 7","This is a test of the create event","At Work",(DateTime.Now ),(DateTime.Now + 3600000),False)
Hope it is of use to someone, it has lots of expansion capability, update calendar event, add more fields to the create event routine, search on different fields etc.
Edward
Edit; Version 1.1 added,
- New module added to create events with reminder
- Added a recurring Rule field to the Create Event modules, see RFC 5545 - Internet Calendaring and Scheduling Core Object Specification (iCalendar) for all the many options available, it is then passed as a string e.g. "FREQ=MONTHLY;WKST=SU;BYDAY=2WE"
- Calendar Example attached, this demonstrates
- Locating Calendar ID
- Getting list of events for the selected calendar for the previous month.
- Creating an event with a reminder
- Creating an all day event
- Creating a recurring event
A Preference Screen (AHPreferenceActivity) is also created to show how the calendar selection can be achieved thru a settings page on menu press.
Edit; Version 1.2 added.
- Updated to include the new Calendar API for Android 4 aka ICS
- Example program now shows events in a listview, a long_click gives the option to delete the event.
- To use the example program select the radio button for the required action then select the calendar you want this action on. The event list is not dynamic so after adding or deleting an event you will have to select read events for that calendar to update the listview.
Edit; Version 1.3 added see post #39 for details
- New function 'ListExtendedCalendarEntryDetails(Value)' this returns a Map containing
* hasAlarm
* recurRule
* duration
* minutes
* method
* AttendeeName
* AttendeeEmail
* AttendeeStatus - Sorted bug where ICS did not get the recurring events.
Edit; Version 1.4 added
The bug that I thought I had fixed in v1.3 was not fixed this sorts it out (I hope)
Edit; Version 1.5 added
Details for recurring event now show the start and end time for the recurring event not the original event.
Edit; Version 1.6 added
Changed library to a process object. (no longer an activity object)
Edit; Version 1.7 added
Bug fixed where event timezone was not entered on pre 4.0 android non "all day" event.
Edit; Version 1.8 added
No change in functionality but now the events are returned sorted based on the date/time of the start of the event.
Edit; Version 1.9 added
The 2 create event methods now return the created event ID as a string
Edit; Version 1.11 added
Attached is an updated version of the library with 2 new functions;
GetEventDetails & UpdateEvent.
At the moment UpdateEvent allows you to change the
1) Title
2) Description
3) Location
4) StartTime
5) EndTime
If you do not want to change any of the first 3 pass "" for the relevant one.
To change the time set the ChangeTime Boolean to 'true' and pass both the start & end times. Both must be passed as the system can update both or neither not just one. If ChangeTime is set to 'false' a value must be still be entered in the StartTime & EndTime fields as the function still expects values even if they are not used. Normally I would not use the Boolean and just pass -1 for the time to indicate that it is not to be used but -1 is a valid time (1 millisecond before the epoch Jan 01 1970).
Fixed a bug where 'GetListOfAllCalendars(False)' only returned your "owned" calendars instead of all calendars.
Edit; Version 1.12 added See this post for some more detail
Added function 'GetExtendedListOfAllCalendars(int)'
This returns a LIST with the following calendar values
- calID (long)
- name (string)
- displayName (string)
- colourName (string)
- location (string)
- timezone (string)
- accesslevel (string)
- owner account (string)
The Returned LIST can contain no calendars to many calendars depending on the Access Level used. If there is more then 1 calendar the group of 8 values for the additional calendars is repeated.
Attachments
Last edited: