problem in deleting event from calendar
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2742778/android-calendar-delete-event">android calendar delete event</a>  </p>
</blockquote>



<p>Actually i write a code for inserting and deleting event from the calendar.The code i have written does the following jobs.</p>

<ol>
<li><p>Before inserting new event in the device calendar it will delete all the event from the device calendar that are put by my app and insert new event in the calendar.So the code first include deleting of all event from the calendar that are put by my app and insert new event.All works fine but sometime it cant delete all the event from the calendar and this happen sometime only and as a result duplicate element insert in my device calendar.So why these happen can anyone please suggest me..i have send my code..</p>

<pre><code>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 &gt; 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 &amp;&amp; !m_bSafeAlert &amp;&amp; !m_bUnsafeAlert &amp;&amp; !m_bFertileAlert &amp;&amp; !m_bPMSAlert)
    m_reminderText = "LoveCycles";

if(bDeleteAndReturn == false)
{
    if (m_bNextCycleAlert &amp;&amp; m_iStart &gt; 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 &gt; 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 &gt; 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 &gt; 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 &gt; 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 &amp;&amp; m_iFertile &gt; 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);
    }
}}
</code></pre></li>
</ol>
