Developer Khalid N. contributed an Android calendar event manager class and demo for Delphi XE6 Firemonkey. The class wraps the Android calendar provider which you can find out more about here. It uses the JNI to access the information and is a good example of how to use the Content Resolver in Delphi. The content resolver is also used for accessing other things like the device contacts. In order to access the calendar you need to have read and write permissions set in your Android manifest (android.permission.READ_CALENDAR and android.permission.WRITE_CALENDAR). It has functions to get the list of available calendars, add a reminder, update a reminder, delete a reminder, and set the completed state of a reminder. The code should work with a few minor changes in Delphi XE5 and AppMethod. The content Here is a sample function from the code where it demonstrates how to use the Content Resolver:
function TCalendarEventsManager.IsAppCalendarExist: integer;
var
wValues : TJavaObjectArray<JString>;
wArgs : TJavaObjectArray<JString>;
wCursor : JCursor;
wFilter : JString;
begin
Result := -1;
try
wValues := TJavaObjectArray<JString>.Create(2); //Extracting only calendar name
wValues[0] := StringToJString('_id');
wValues[1] := StringToJString('account_name');
wFilter := StringToJString('account_type = ? AND account_name = ?'); //filter
wArgs := TJavaObjectArray<JString>.Create(2); // Arguments to filter calendars list
wArgs[0] := StringToJString('LOCAL');
wArgs[1] := StringToJString(_App_Calendar_Name);
wCursor := fContentResolver.query(StrToJURI('content://com.android.calendar/calendars'),wValues,wfilter,wArgs,nil);
if wCursor.moveToFirst then
Result := wCursor.getLong(0);
except
On E:Exception do
Raise Exception.create('[TCalendarEventsManager.IfAppCalendarExist] : '+E.message);
end;
end;
Download the Calendar Event Manager demo for Delphi XE6 Firemonkey.
This solution does not work. When you create a calendar, an error is returned:
“[TCalendarEventsManager.Create]
No calendar found on the device”
P.S. It is tested on XE7 and android virtul device.
The error sounds pretty clear. The virtual device has no calendar? Maybe a Calendar app has to be installed?
I added the following code TCalendarEventsManager.Create at the beginning:
fCalendarID := CreateNewCalendar;
it worked
Thanks
P.S. I added parameters function TCalendarEventsManager.AddNewReminder:
– isAllDay
– BeginTime
– EndTime