将日历和事件的Android 4.0(Adding Calendar and events in A

2019-07-28 21:50发布

我想补充的日历事件在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的太是什么改变?

Answer 1:

在ICS,你必须使用公共日历API。 请检查下面的链接:

  1. 如何阅读和使用新的Android 4.0 Ice Cream Sandwich的API编辑Android的日历事件?

  2. http://www.techrepublic.com/blog/app-builder/programming-with-the-android-40-calendar-api-the-good-the-bad-and-the-ugly/825

  3. http://android10.org/index.php/articlestrickssecrets/353-android-ics-adding-events-to-the-calendar

  4. http://www.vogella.com/articles/AndroidCalendar/article.html



Answer 2:

我认为,“知名度”并不在Android 4.0的存在

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

能比好

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



Answer 3:

我得到了同样的问题及其解决方案too.My事件ICS和豆形软糖设备没有增加,但对所有其他工作。
试试这个 -

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

使用能见度只有当设备版本低于14(ICS)



文章来源: Adding Calendar and events in Android 4.0