我通过组合日历事件表的 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();
}
}
}