3

我们正在开发一个可以在所有版本的 Android 操作系统上运行的应用程序。

在我们的应用程序中,我们需要获取日历事件,这些事件将从手机随附的默认日历应用程序中添加。

我已经检查了 CalendarContract.Calendars、CalendarContract.Events 和 CalendarContract.Attendees 表。在 Calendars 表中没有事件 id 。但是在事件和与会者表中 event_id 在那里。如果我们在 Galaxy Nexus 的默认日历应用程序中插入日历事件,那么它会将 event_id 插入为 1、2、3、4。但是在 Android 2.1 中,我们得到了一个名为 iCalGuid 的优秀字段......我们得到了独特的日历 eventId,如 GUID 字段......他们有什么方法可以在较低版本中将 Android 4.0 中的日历事件 ID 作为 iCalGuid 获取?

4

1 回答 1

2

我通过组合日历事件表的 5 列来创建唯一 id 解决了这个问题 -----

首先设置日历URI值如下---

Uri calenderContentUri;
if(Build.VERSION.SDK_INT >= 8)
{
     calenderContentUri = Uri.parse("content://com.android.calendar/events");
}
else
{
    calenderContentUri = Uri.parse("content://calendar/events");
}

注意 :: 不要使用像 Events.CONTENT_URI 这样的常量值。取而代之的是,如上所示设置 uri。

接下来,我通过组合 Event 表的 5 列(title、ownerAccount、eventLocation、dtstart、dtend)来制作自定义复合唯一键。我在任何地方都在使用这个 id,它解决了我的问题。

if (Build.VERSION.SDK_INT >= 14)
                    {
                        String[] projection = null;
                        //By default  you have the following calendars in your android phone like 
                        //My Calendar, Gmail calendar,  Indian holydays or if you have installed some
                        //third party calendar app etc. They will be automatically given an unique id by OS.
                        //In the next line "selectedCalenderId" is indicating a perticular calendar like My Calendar or Gmail calendar.
                        //You need to write seperate code to get those particular calendar ids. here "selectedCalenderId" is that type of id(in my case value may be 0(My Calendar). 1(Gmail Calendar), or 3(Indian Holydays))
                        String selection = "calendar_id=" +selectedCalenderId +" and dtstart between ? and ?";

                        //provide the time range(in system mili seconds)  from what you want to get the events.
                        String[] selectionArgs = new String[] {getTimeInMilisecond(day + " 00:00:00"), getTimeInMilisecond(day + " 23:59:59")};

                        Cursor cursor = null;

                        try
                        {
                            cursor = getContentResolver().query(calenderContentUri, projection, selection, selectionArgs, "dtstart DESC, dtend DESC");
                            if (cursor.moveToFirst())
                            {
                                int increment = 0;

                                String eventName;
                                String calendarOwnerName;
                                String location;
                                long eventBeginTime;
                                long eventEndTime;

                                String description;
                                String calendarSyncId;

                                String customCalendarEventId ;

                                do
                                {
                                    eventName = cursor.getString(cursor.getColumnIndex("title"));
                                    eventBeginTime = cursor.getLong(cursor.getColumnIndex("dtstart"));
                                    eventEndTime = cursor.getLong(cursor.getColumnIndex("dtend"));
                                    location = cursor.getString(cursor.getColumnIndex("eventLocation"));
                                    calendarOwnerName = cursor.getString(cursor.getColumnIndex("ownerAccount"));
                                    description = cursor.getString(cursor.getColumnIndex("description"));

                                    //Watch that I am combining the 4 columns
                                    calendarSyncId = eventName +"-" +calendarOwnerName +"-" +location +"-" +eventBeginTime+"-" +eventEndTime;

                                    //Making MD5 encryption to use it as unique key
                                    customCalendarEventId =  yourMD5EncryptionLogic(calendarSyncId);

                                    //TODO ::: Do the rest of your coding for OS version higher than 14.
                                }
                                while(cursor.moveToNext());
                            }
                        }
                        finally  {
                            if(cursor != null) {
                                cursor.close();
                            }
                        }
              }
              else
              {
                    String[] projection = null;
                    //By default  you have the following calendars in your android phone like 
                        //My Calendar, Gmail calendar,  Indian holydays or if you have installed some
                        //third party calendar app etc. They will be automatically given an unique id by OS.
                        //In the next line "selectedCalenderId" is indicating a perticular calendar like My Calendar or Gmail calendar.
                        //You need to write seperate code to get those particular calendar ids. here "selectedCalenderId" is that type of id(in my case value may be 0(My Calendar). 1(Gmail Calendar), or 3(Indian Holydays))
                    String selection = "calendar_id=" +selectedCalenderId +" and dtstart between ? and ?";
                    String[] selectionArgs = new String[] {getTimeInMilisecond(day + " 00:00:00"), getTimeInMilisecond(day + " 23:59:59")};

                    Cursor cursor = null;

                    try
                    {
                        cursor = getContentResolver().query(calenderContentUri, projection, selection, selectionArgs, "dtstart DESC, dtend DESC");
                        if (cursor.moveToFirst())
                        {
                            int increment = 0;

                            String eventName;
                            String calendarOwnerName;
                            String location;

                            long eventBeginTime;
                            long eventEndTime;

                            String description;
                            String calendarGuid;

                            String customCalendarEventId;

                            int selfAttendanceStatusVal = -1;

                            do
                            {
                                selfAttendanceStatusVal = cursor.getInt(cursor.getColumnIndex("selfAttendeeStatus"));
                                eventName = cursor.getString(cursor.getColumnIndex("title"));
                                eventBeginTime = cursor.getLong(cursor.getColumnIndex("dtstart"));
                                eventEndTime = cursor.getLong(cursor.getColumnIndex("dtend"));
                                location = cursor.getString(cursor.getColumnIndex("eventLocation"));
                                calendarOwnerName = cursor.getString(cursor.getColumnIndex("ownerAccount"));
                                description = cursor.getString(cursor.getColumnIndex("description"));

                                //Watch that I am combining the 4 columns
                                calendarGuid = eventName +"-" +calendarOwnerName +"-" +location +"-" +eventBeginTime+"-" +eventEndTime;

                                //Making MD5 encryption to use it as unique key
                                customCalendarEventId =  yourMD5EncryptionLogic(calendarSyncId);

                                //TODO ::: Do the rest of your coding for OS version less than 14.
                            }
                            while(cursor.moveToNext());
                            }
                        }
                        finally  {
                            if(cursor != null) {
                                cursor.close();
                            }
                        }
              }
于 2013-04-12T07:45:56.127 回答