简体   繁体   English

如何在Android日历中检索最近添加的事件

[英]How to retrieve lastly added events in android calendar

According to my requirements i should save the events in android calendar like event name, starDate and endDate etc and retrieve lastly saved events, Now everything goes well, instead of fetching last event values i'm getting first event, can anybody tell the logic for this issue please. 根据我的要求,我应该将事件保存在android日历中,例如事件名称,starDate和endDate等,并检索最近保存的事件,现在一切顺利,而不是获取我得到的第一个事件的最后一个事件值,任何人都可以告诉逻辑请问这个问题。

Here's my code 这是我的代码

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

Retrieving code 检索代码

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

retrieve lastly saved events 检索最近保存的事件

As long as you don't save the date where you add the event you can't get last added events. 只要您不保存添加事件的日期,就无法获取上次添加的事件。


Looking in Calendar.Contract.Events API I can't find any field to insert this kind of extra data, so you have 2 sol.lutions: Calendar.Contract.Events API中查找我找不到任何字段来插入这种额外的数据,因此您有2个sol.lutions:

  • Create a SharedPreferences or DataBase to store basic data to find a calendar event PLUS adding date, then make search in this Preferences or DataBase and retrieve information from calendar according to it. 创建SharedPreferencesDataBase存储基本数据,以寻找PLUS日历事件添加日期,然后在这首或数据库搜索和检索根据从日历信息。

  • EASIER, BUT NOT RECOMMENDED 更轻松,但不推荐
    Add in some String field like DESCRIPTION or ORGANIZER the Timestamp or Date of addition of the event, then when searching just get this part of the event and sort to get last added. 在一些String字段中添加例如DESCRIPTIONORGANIZER的事件的TimestampDate ,然后在搜索时仅获取事件的这一部分并进行排序以获取最后添加的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM