我想在 android 4.0 及以上版本的设备中添加日历事件。目前我正在使用以下代码添加事件,
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());
}
}
}
}
上面的代码在 android 3.0 之前运行良好,但是在 android 4.0 中没有添加事件,这是我从我的客户那里得到的投诉。我没有android 4.0的设备,所以我无法检查它。
对于它的 android 设备,sdk version 7 and below
我们用作
Uri calendars = Uri.parse("content://calendar/calendars");
至于sdk version above 7
我们在哪里使用
Uri calendars= Uri.parse("content://com.android.calendar/calendars");
这对于 android 4.0 是否也一样,有什么要改变的吗?