游标中存在已删除日历的事件

时间:2014-03-21 08:53:24

标签: android calendar

我在我的Android日历中添加了事件,然后通过我的应用程序我分析它们是否完美。

但是当我从日历中删除事件然后从日历中读取事件时。它仍然向我展示这些事件。我只是想知道为什么会出现这种行为。

我正在使用此代码阅读最新的活动。

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();
    }

它也向我显示已删除的事件。

1 个答案:

答案 0 :(得分:0)

确切的语法如下:

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

对我有用!!!
完整代码在这里

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   
相关问题