Android CalendarContract.Instances table returns old values

断了今生、忘了曾经 提交于 2021-01-28 11:37:30

问题


I am querying the CalendarContract.Instances table to read all calendar event instances within a time interval (curr_time, next_time), as shown in the code below. This code works when the calendar entries have been around for a while. However, if I add a new entry, change an existing entry, or delete it, I get some old/stale entries (e.g., a new entry is not returned when it should be, or the unmodified/deleted entry is still returned), and this happens consistently. This problem occurs on my phone (Samsung S7) but does not appear to occur on the Android emulator. I am wondering if anyone else has seen this problem, or what I might be doing wrong. Any help is appreciated. Thanks.

try {
    Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
    ContentUris.appendId(builder, curr_time);
    ContentUris.appendId(builder, next_time);

    String[] INSTANCE_PROJECTION = new String[]{Instances.EVENT_ID,
            Instances.TITLE, Instances.BEGIN, Instances.END};

    Uri uri = builder.build();
        cur = cr.query(uri, INSTANCE_PROJECTION, null, null,null);

    if (cur != null) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(Instances.EVENT_ID));
            String title = cur.getString(cur.getColumnIndex(Instances.TITLE));
            long start = cur.getLong(cur.getColumnIndex(Instances.BEGIN));
            long end = cur.getLong(cur.getColumnIndex(Instances.END));

            Calendar calendar = Calendar.getInstance();
            DateFormat formatter = SimpleDateFormat
                    .getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
            calendar.setTimeInMillis(start);
            Date start_date = calendar.getTime();
            calendar.setTimeInMillis(end);
            Date end_date = calendar.getTime();

            Log.i(log_tag, "Event:  " + id + "\t" + title + "\t" +
                    start + " " + formatter.format(start_date) + "\t" +
                    end + " " + formatter.format(end_date));
        }
    }
} catch (SecurityException e) {
    // no permission to read calendars
} finally {
    if (cur != null)
        cur.close();
}

来源:https://stackoverflow.com/questions/60082986/android-calendarcontract-instances-table-returns-old-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!