如何获取Android 4.0的唯一日历活动ID?

时间:2012-08-20 15:15:57

标签: android-calendar

我们正在开发一个可在所有Android OS版本上运行的应用程序。

在我们的应用中,我们需要提取日历活动,这些活动将从手机附带的默认日历应用中添加。

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

1 个答案:

答案 0 :(得分: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。

下一步我通过合并事件表的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();
                            }
                        }
              }