2

我通过创建帐户在我自己的日历中添加了事件,但是当我通过同步和帐户管理器通过同步日历进行同步时,事件被重复所以我如何检查事件是否已经存在。我已经优化了以下代码,但似乎无法正常工作。

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;
        }
4

2 回答 2

4

我如何实施另一种方法来检查事件是否已在日历中注册...

public boolean isEventInCal(Context context, String cal_meeting_id) {

 Cursor cursor = context.getContentResolver().query(
 Uri.parse("content://com.android.calendar/events"),
   new String[] { "_id" }, " _id = ? ",
   new String[] { cal_meeting_id }, null);

       if (cursor.moveToFirst()) {
  return true;
 }
 return false;
}
于 2013-08-07T04:38:59.750 回答
1

我做的一件简单的事情(对于警报)是查询是否已经存在具有相同属性的事件,并且仅在不存在时才添加它。这也使您可以更新现有条目。

于 2013-07-29T10:52:04.117 回答