Reputation: 4968
I want to add calendar events in android 4.0 and above version device. Currently i am adding the events using the following code,
if (android.os.Build.VERSION.SDK_INT <= 7 )
{
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = managedQuery(calendars, projection, null, null, null);
if (managedCursor.moveToFirst())
{
String calName;
String calId;
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
if(calId != null)
{
try
{
Log.e("Calendar Id : ",""+calId+" : "+calName);
ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", summary);
event.put("description", summary);
event.put("eventLocation", "");
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", allDayFlag);
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
Log.e("Event Res : ",""+url);
if(!url.equals(""))
Main.showErrorDialog(this, "Event Successfully Added ");
}
catch (Exception kwse)
{
Log.e("Exception 1 kwse ",""+kwse.toString());
}
}
}
}
else
{
Uri calendars= Uri.parse("content://com.android.calendar/calendars");
Cursor managedCursor = managedQuery(calendars, projection, null, null, null);
if (managedCursor.moveToFirst())
{
String calName;
String calId;
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
if(calId != null)
{
try
{
Log.e("Calendar Id : ",""+calId+" : "+calName);
ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", summary);
event.put("description", summary);
event.put("eventLocation", "");
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", allDayFlag);
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("hasAlarm", 1);
Uri eventsUri = Uri.parse("content://com.android.calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
Log.e("Event Res : ",""+url);
if(!url.equals(""))
Main.showErrorDialog(this, "Event Successfully Added ");
}
catch (Exception kwse)
{
Log.e("Exception 2 kwse ",""+kwse.toString());
}
}
}
}
The above code is running good till android 3.0, but the events are not getting added in android 4.0, this is a complaint i got from my client. I dont have a device of android 4.0, so i am unable to check it.
For android devices of sdk version 7 and below
it we use as
Uri calendars = Uri.parse("content://calendar/calendars");
where as for sdk version above 7
we use as
Uri calendars= Uri.parse("content://com.android.calendar/calendars");
Is this is the same for android 4.0 too are anything to be changed ?
Upvotes: 2
Views: 8079
Reputation: 6707
I got the same problem and its solution too.My events was not adding in ICS and JellyBean devices but working for all other.
Try this -
ContentValues event = new ContentValues();
int apiLevel = android.os.Build.VERSION.SDK_INT;
if(apiLevel<14)
event.put("visibility", 0);
Use visibility only if device version is less than 14(ICS)
Upvotes: 1
Reputation: 61
I think "visibility" doesn't exist in Android 4.0
And
int nameColumn = managedCursor.getColumnIndex("calendar_displayName");
could be better than
int nameColumn = managedCursor.getColumnIndex("name");
Upvotes: 3
Reputation: 2020
In ICS you have to use the public Calendar API. Please check the links below:
How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?
http://android10.org/index.php/articlestrickssecrets/353-android-ics-adding-events-to-the-calendar
http://www.vogella.com/articles/AndroidCalendar/article.html
Upvotes: 0