如何在android日历中检索最后添加的事件

时间:2015-09-15 07:29:47

标签: java android calendar google-calendar-api android-calendar

根据我的要求,我应该将事件保存在android日历中,如事件名称,starDate和endDate等,并检索最后保存的事件,现在一切顺利,而不是获取最后的事件值我得到第一个事件,可以任何人都要告诉这个问题的逻辑。

这是我的代码

 private void addEvent2() {
    Intent l_intent = new Intent(Intent.ACTION_EDIT);
    l_intent.setType("vnd.android.cursor.item/event");
    //l_intent.putExtra("calendar_id", m_selectedCalendarId);  //this doesn't work

    l_intent.putExtra("title", "roman10 calendar tutorial test");
    l_intent.putExtra("description", "This is a simple test for calendar api");
    l_intent.putExtra("eventLocation", "@home");
    l_intent.putExtra("beginTime", System.currentTimeMillis());
    l_intent.putExtra("endTime", System.currentTimeMillis() + 1800*1000);
    l_intent.putExtra("allDay", 0);
    //status: 0~ tentative; 1~ confirmed; 2~ canceled
    l_intent.putExtra("eventStatus", 1);
    //0~ default; 1~ confidential; 2~ private; 3~ public
    l_intent.putExtra("visibility", 3);
    //0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
    l_intent.putExtra("transparency", 0);
    //0~ false; 1~ true
    l_intent.putExtra("hasAlarm", 1);
    try {
        startActivity(l_intent);
    } catch (Exception e) {
        Toast.makeText(this.getApplicationContext(), "Sorry, no compatible calendar is found!", Toast.LENGTH_LONG).show();
    }
}

检索代码

private void getLastEvent() {
    Uri l_eventUri;
    if (Build.VERSION.SDK_INT >= 8) {
        l_eventUri = Uri.parse("content://com.android.calendar/events");
    } else {
        l_eventUri = Uri.parse("content://calendar/events");
    }
    String[] l_projection = new String[]{"title", "dtstart", "dtend"};
    Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, "calendar_id=" + m_selectedCalendarId, null, "dtstart DESC, dtend DESC");
    //Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, null, null, null);
    if (l_managedCursor.moveToFirst()) {
        int l_cnt = 0;
        String l_title;
        String l_begin;
        String l_end;
        StringBuilder l_displayText = new StringBuilder();
        int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
        int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
        int l_colEnd = l_managedCursor.getColumnIndex(l_projection[1]);
        do {
            l_title = l_managedCursor.getString(l_colTitle);
            l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
            l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
            l_displayText.append(l_title + "\n" + l_begin + "\n" + l_end + "\n----------------\n");
            ++l_cnt;
        } while (l_managedCursor.moveToNext() && l_cnt < 1);
        m_text_event.setText(l_displayText.toString());
                }
    l_managedCursor.close();
}

1 个答案:

答案 0 :(得分:0)

  

检索最后保存的事件

只要您不保存添加活动的日期,就无法获得最后添加的活动。

查看Calendar.Contract.Events API我无法找到任何字段来插入此类额外数据,因此您有2个解决方案:

  • 创建SharedPreferencesDataBase以存储基本数据以查找日历事件 PLUS 添加日期,然后在此偏好设置或数据库中进行搜索并检索信息根据它来自日历。

  • 更容易,但不推荐
    添加一些String字段,例如DESCRIPTIONORGANIZER TimestampDate添加了该事件,然后在搜索时获取此部分事件并排序以获得最后一次添加。