Adding Calendar and events in Android 4.0

好久不见. 提交于 2019-12-21 06:28:55

问题


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 ?


回答1:


In ICS you have to use the public Calendar API. Please check the links below:

  1. How to read and edit Android calendar events using the new Android 4.0 Ice Cream Sandwich API?

  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




回答2:


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");




回答3:


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)



来源:https://stackoverflow.com/questions/10100563/adding-calendar-and-events-in-android-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!