我通过创建帐户在我自己的日历中添加了事件,但是当我通过同步和帐户管理器通过同步日历进行同步时,事件被重复所以我如何检查事件是否已经存在。我已经优化了以下代码,但似乎无法正常工作。
Calendar cal = Calendar.getInstance();
Calendar beginTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();
Date dt = null;
Date dt1 = null;
try {
dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(Stime);
dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(Etime);
cal.setTime(dt);
// beginTime.set(2013, 7, 25, 7, 30);
beginTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DATE), cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE));
cal.setTime(dt1);
// endTime.set(2013, 7, 25, 14, 30);
endTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DATE), cal.get(Calendar.HOUR),
cal.get(Calendar.MINUTE));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ContentResolver cr = this.mContext.getContentResolver();
ContentValues values = new ContentValues();
String selection = "((" + Instances.DTSTART + " = '"
+ beginTime.getTimeInMillis() + "') AND ("
+ Instances.DTEND + " = '" + endTime.getTimeInMillis()
+ "') AND (" + Instances.TITLE + " = '" + EventName
+ "') AND (" + Instances.CALENDAR_ID + " = '"
+ cal_event_id + "'))";
Log.i("EVENT NAME", " " + EventName);
Log.i("EVENT STIME", " " + Stime);
Log.i("EVENT ETIME", " " + Etime);
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, beginTime.getTimeInMillis());
ContentUris.appendId(builder, endTime.getTimeInMillis());
long begin = beginTime.getTimeInMillis();// starting time in
// milliseconds
long end = endTime.getTimeInMillis(); // ending time in milliseconds
String[] proj = new String[] { Instances._ID, Instances.DTSTART,
Instances.DTEND, Instances.EVENT_ID, Instances.TITLE,
Instances.CALENDAR_ID };
Cursor cursor = Instances.query(this.mContext.getContentResolver(),
proj, begin, end);
if (cursor.getCount() > 0) {
// deal with conflict
while (cursor.moveToNext()) {
long id = 0;
id = cursor.getLong(cursor.getColumnIndex("_ID"));
long eventid = 0;
eventid = cursor.getLong(cursor.getColumnIndex("EVENT_ID"));
String event_Name = null;
event_Name = cursor.getString(cursor
.getColumnIndex("TITLE"));
Log.i("event_Name", event_Name);
long Calid = 0;
Calid = cursor
.getLong(cursor.getColumnIndex("CALENDAR_ID"));
if (event_Name.equalsIgnoreCase(EventName)) {
String[] j = cursor.getColumnNames();
Log.i("DUPLICATE", "-------- " + EventName + "ID:::"
+ id + "==" + j[1] + "==" + j[2] + "EVENTID::"
+ eventid + "==" + j[4] + "CALENDARID::"
+ Calid);
return 1;
} else {
Log.i("LATEST", "CAST");
return 0;
}
}
} else {
Log.i("LATEST", "-------- " + cursor.getCount());
}
return 0;
}