4

I added events in my android calendar and then by my app I analyze them working perfect.

But when I deleted events from my calendar and then reading events from calendar. It is still showing me those event. I just want to know why it is showing this behavior.

I am using this code to read the latest event available.

private void fetchCalenderEvents() {

        Cursor cur = null;
        ContentResolver cr = getContentResolver();
        cur = cr.query(CalendarContract.Events.CONTENT_URI, EVENT_PROJECTION,
                null, null,null);

        outputText.setText("");

        long currenttime = new Date().getTime();
        //setContentView(R.layout.main);
        while (cur.moveToNext()) {

            StringBuilder strbuldr = new StringBuilder();
            long calID = 0;
            String displayName = null;
            String accountName = null;
            String ownerName = null;
            String name = null;
            long startDate,endDate;
            String eventlocation = null;
            String eventOrganizer = null;
            String strDuration = null;

            // Get the field values
            calID = cur.getLong(PROJECTION_ID_INDEX);
            displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
            accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
            ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
            name = cur.getString(PROJECTION_EVENT_TITILE);

            startDate = cur.getLong(PROJECTION_EVENT_DTSTART);
            endDate = cur.getLong(PROJECTION_EVENT_DATEEND);

            Format df = DateFormat.getDateFormat(this);
            Format tf = DateFormat.getTimeFormat(this);

            eventlocation = cur.getString(PROJECTION_EVENT_LOC);
            eventOrganizer = cur.getString(PROJECTION_EVENTT_ORGANIZER);
            strDuration = cur.getString(PROJECTION_EVENT_DURATION);
            if(startDate>=currenttime){
                strbuldr.append("Meeting "+name 
                                        +" "
                                        +"has been Scheduled on date"+df.format(startDate) 
                                        +" time "
                                        +tf.format(startDate) 
                                        +" for duration "+strDuration);


                outputText.append(strbuldr.toString());
                outputText.append("\n");
                if (outputText.getText().toString().trim().length() == 0) {
                    Toast.makeText(getApplicationContext(),
                            "Please enter your message.", Toast.LENGTH_LONG).show();
                    return;
                }
                else{
                    speakUSLocale();
                    confirmTTSData();
                    break;
                }


            }else{
                Log.d(DEBUG_TAG, "Old events");
            }
        }

        cur.close();
    }

It is showing me deleted events also.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Java_Alert
  • 1,159
  • 6
  • 24
  • 50
  • 1
    Hi, I got the solution we have to add String selection = "(deleted != 1)"; in the contentresolver.query(....). – Java_Alert Mar 23 '14 at 05:57

1 Answers1

1

the exact syntax looks like this:

String selection = "(   ( " + CalendarContract.Events.DTSTART + " >= " + beginTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() +   " ) AND ( " + CalendarContract.Events.DELETED + "  != 1) " +   "     )";

that worked for me!!!
full code here

public void mycheckforappointments() {
    boolean foundappointment = false;
    int calId = 0;
    String title = "";
    String msg = "";
    String description = "";
    Date startime;
    Date endtime;
    String starttimestring;
    Cursor cursor = null;
    String[] projection = new String[]{CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION};

    // 0 = January, 1 = February, ...

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2019, 1, 20, 7, 30);
    Calendar endTime = Calendar.getInstance();
    endTime.set(2019, 1, 27, 8, 30);
    beginSavedTime = beginTime;
    beginEndTime = endTime;

    // the range
    try {
        //String OLDselection = "(( " + CalendarContract.Events.DTSTART + " >= " + beginTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";
        String selection = "(   ( " + CalendarContract.Events.DTSTART + " >= " + beginTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DELETED + "  != 1) " + "     )";

        cursor = this.getBaseContext().getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);

        if (cursor.moveToFirst()) {
            do {
                calId = cursor.getInt(0);
                title = cursor.getString(1);
                description = cursor.getString(2);
                startime = new Date(cursor.getLong(3));

                if (title.equals("XXXX Community Services Appointment")) {

                    startime = new Date(cursor.getLong(3));
                    starttimestring = startime.toString();
                    TextView myappointmentstattime = (TextView) findViewById(R.id.myappointmentstartime);
                    myappointmentstattime.setText(starttimestring);

                    description = cursor.getString(2);
                    TextView myappointmentdescription = (TextView) findViewById(R.id.myappointmentdescription);
                    myappointmentdescription.setText(description);
                    foundappointment = true;
                }

            } while (cursor.moveToNext());
        }
        if (!foundappointment)//didnt find one
        {
            msg = "Did not find any XXX Community Services Appointments";
            displayMsg("XXX App", msg);

        }
    } catch (Exception e) {
        msg = e.toString();
        displayMsg("Error", msg);
        return;
    }
}//end