2

我想在 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 是否也一样,有什么要改变的吗?

4

3 回答 3

3

我认为 Android 4.0 中不存在“可见性”

int nameColumn = managedCursor.getColumnIndex("calendar_displayName");

可能比

int nameColumn = managedCursor.getColumnIndex("name");

于 2012-05-11T12:37:18.163 回答
1

我也遇到了同样的问题及其解决方案。我的事件没有添加到 ICS 和 JellyBean 设备中,而是适用于所有其他设备。
试试这个 -

ContentValues event = new ContentValues();
int apiLevel = android.os.Build.VERSION.SDK_INT;
            if(apiLevel<14)
            event.put("visibility", 0);

仅当设备版本低于 14(ICS) 时才使用可见性

于 2013-02-04T13:46:23.950 回答