实际上我编写了一个用于在日历中插入和删除事件的代码。我编写的代码执行以下工作。
在设备日历中插入新事件之前,它会删除我的应用程序放置的设备日历中的所有事件,并在日历中插入新事件。因此代码首先包括从日历中删除所有事件这是由我的应用程序和插入新事件。所有工作正常,但有时它不能删除日历中的所有事件,这只发生一次,因此重复元素插入我的设备日历。所以为什么这些发生可以任何人请建议我......我发了我的代码..
public void setAlertOnDevice(Context c)
{
Resources res = c.getResources();
Uri EVENTS_URI = Uri.parse("content://com.android.calendar/" + "events");
Uri REMINDERS_URI = Uri.parse("content://com.android.calendar/" + "reminders");
ContentResolver cr = c.getContentResolver();
//Delete calendar event
while(m_calendarEventCount > 0)
{
cr.delete(m_calendarEvents[m_calendarEventCount - 1], null, null);
m_calendarEventCount = m_calendarEventCount - 1;
}
//set Alerts in device calendar
Date dtStartDate = getStartDate();
boolean bDeleteAndReturn = false;
Calendar cal = Calendar.getInstance();
if (dtStartDate.getTime() == getDefaultDate().getTime())
{
bDeleteAndReturn = true;
dtStartDate = cal.getTime();
}
getOffsetsForCycleStages(m_iAvgCycleTime);
if(!m_bNextCycleAlert && !m_bSafeAlert && !m_bUnsafeAlert && !m_bFertileAlert && !m_bPMSAlert)
m_reminderText = "LoveCycles";
if(bDeleteAndReturn == false)
{
if (m_bNextCycleAlert && m_iStart > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iStart);
String str = m_reminderText + res.getString(R.string.alert_start);
m_strDescription = res.getString(R.string.alert_start_msg);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
if (m_bSafeAlert)
{
if (m_iSafe1 > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iSafe1);
String str = m_reminderText + res.getString(R.string.alert_safe);
m_strDescription = res.getString(R.string.alert_safe_msg) + " " + new Integer(m_iUnsafe1-m_iSafe1-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
if (m_iSafe2 > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iSafe2);
String str = m_reminderText + res.getString(R.string.alert_safe);
m_strDescription = res.getString(R.string.alert_safe_msg) + " " + new Integer(CycleManager.getSingletonObject().m_iAvgCycleTime-m_iSafe2-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
}
if (m_bUnsafeAlert)
{
if (m_iUnsafe1 > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iUnsafe1);
String str = m_reminderText + res.getString(R.string.alert_unsafe);
m_strDescription = res.getString(R.string.alert_unsafe_msg) + " " + new Integer(m_iFertile-m_iUnsafe1-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
if (m_iUnsafe2 > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iUnsafe2);
String str = m_reminderText + res.getString(R.string.alert_unsafe);
m_strDescription = res.getString(R.string.alert_unsafe_msg) + " " + new Integer(m_iSafe2-m_iUnsafe2-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
}
if (m_bFertileAlert && m_iFertile > 0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iFertile);
String str=m_reminderText + res.getString(R.string.alert_fertile);
m_strDescription = res.getString(R.string.alert_fertile_msg) + " " + new Integer(m_iUnsafe2-m_iFertile-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert(REMINDERS_URI, values);
}
if (m_bPMSAlert)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, m_iStart - 7);
String str = m_reminderText + res.getString(R.string.alert_pms);
m_strDescription = res.getString(R.string.alert_pms_msg);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", m_strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
m_calendarEvents[m_calendarEventCount] = event;
m_calendarEventCount = m_calendarEventCount + 1;
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", 10);
cr.insert( REMINDERS_URI, values);
}
}}