3

すべてのバージョンの Android OS で動作するアプリケーションを開発しています。

このアプリでは、カレンダー イベントを取得する必要があります。これらのイベントは、携帯電話に付属しているデフォルトのカレンダー アプリから追加されます。

CalendarContract.Calendars、CalendarContract.Events、CalendarContract.Attendees テーブルは既にチェック済みです。Calendars テーブルには event id がありません。しかし、Events テーブルと Attendees テーブルには event_id があります。Galaxy Nexus のデフォルトのカレンダー アプリにカレンダー イベントを挿入すると、event_id が 1、2、3、4 として挿入されます。しかし、Android 2.1 では、iCalGuid と呼ばれる優れたフィールドを取得しています...ここで、GUID フィールドのような一意のカレンダー eventId を取得しています...下位バージョンの iCalGuid として Android 4.0 でカレンダー イベント ID を取得する方法はありますか?

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 に答える