安卓日历,获取事件ID

11

我正在编写一个需要在 Android 日历中添加事件的应用程序。我只是使用以下代码进行插入:

public void onItemClick(AdapterView<?> adapter, View curview, int position, long id) {
    WhoisEntry entry = this.adapter.getItem(position);      
    String domainName = entry.getDomainName();
    Date expDate = entry.expirationDate;
    Toast.makeText(getApplicationContext(), "Domain: " + domainName, Toast.LENGTH_SHORT).show();
    Calendar cal = Calendar.getInstance();            
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", entry.expirationDate);
    intent.putExtra("allDay", false);       
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "Expiration of " + entry.domainName);
    startActivity(intent);
}

我现在想知道是否有可能获取与该事件相关联的ID,这样在插入事件后,将其ID保存到我的应用程序中后,用户可以直接从应用程序内部调用该事件。

这是否可能?


你确定要将endTime设置为当前时间吗? - toto2
抱歉!这只是我的分心,与编程无关。 - Ivan
如果您正在为ICS开发,那么有一个新的日历API可供使用;请参阅此博客 - toto2
是的,我知道,但我想开发我的应用程序不仅适用于ICS。 - Ivan
2个回答

12

我提取了用于存储Android日历事件的列列表,以下是该列表:

[0] "originalEvent" (id=830007842672)
[1] "availabilityStatus" (id=830007842752)
[2] "ownerAccount" (id=830007842840)
[3] "_sync_account_type" (id=830007842920)
[4] "visibility" (id=830007843008)
[5] "rrule" (id=830007843080)
[6] "lastDate" (id=830007843144)
[7] "hasAlarm" (id=830007843216)
[8] "guestsCanModify" (id=830007843288) [9] "guestsCanSeeGuests" (id=830007843376)
[10] "exrule" (id=830007843464)
[11] "rdate" (id=830007843528)
[12] "transparency" (id=830007843592)
[13] "timezone" (id=830007843672)
[14] "selected" (id=830007843744)
[15] "dtstart" (id=830007843816) [16] "title" (id=830007843888)
[17] "_sync_time" (id=830007843952)
[18] "_id" (id=830007844024) [19] "hasAttendeeData" (id=830007844088) [20] "_sync_id" (id=830007844176)
[21] "commentsUri" (id=830007844248) [22] "description" (id=830007844328) [23] "htmlUri" (id=830007844408) [24] "_sync_account" (id=830007844480)
[25] "_sync_version" (id=830007844560)
[26] "hasExtendedProperties" (id=830007844640)
[27] "calendar_id" (id=830007844736)

如果我想获取我的事件的新事件ID,则可以使用以下方法:

public static long getNewEventId(ContentResolver cr, Uri cal_uri){      
    Uri local_uri = cal_uri;
    if(cal_uri == null){
        local_uri = Uri.parse(calendar_uri+"events");
    } 
    Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
    cursor.moveToFirst();
    long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));     
    return max_val+1;
}

对于插入事件:

public void insertDomainEntry(Date exp_date, String name, long event_id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("exp_date", exp_date.getTime()/1000);
    values.put("event_id", event_id);
    values.put("domainname", name);
    db.insertOrThrow("domains_events", null, values);
}

那个解决方案似乎可以工作,尽管这可能不是一个非常好的解决方案。

编辑02/2015 getNextEventId的目的是为事件表创建一个新的事件条目,以下是使用该方法的代码:

@Override
    public void onItemClick(AdapterView<?> adapter, View curview, int position,
            long id) {
        WhoisEntry entry = this.adapter.getItem(position);      
        long event_id = CalendarUtils.getNewEventId(getContentResolver(), null);

        Toast.makeText(getApplicationContext(), "Domain: " + entry.getDomainName(),
                Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", entry.getExpiration().getTime());
        intent.putExtra("_id", event_id);
        intent.putExtra("allDay", false);       
        intent.putExtra("endTime", entry.getExpiration().getTime()+60*30);
        intent.putExtra("title", "Expiration of " + entry.getDomainName());
        startActivity(intent);

        database.insertDomainEntry(entry.getExpiration(),
                entry.getDomainName(), event_id);
    }

更新 09/2015

根据评论的要求,我添加了如何获取日历URI的说明(基本上是存储日历的位置,应用程序会尝试猜测它,并在所有已知的可能的日历路径中进行搜索)。

public static String getCalendarUriBase(Activity act) {     
    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;

    try {
        managedCursor = act.getContentResolver().query(calendars,
                null, null, null, null);
    } catch (Exception e) {
    }

    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.getContentResolver().query(calendars,
                    null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }

    calendar_uri= calendarUriBase;
    return calendarUriBase;
}

你怎么知道最后一个事件是用户添加的?也许它是旧事件?(用户取消了) - dowi
为什么我需要了解最后一个事件?当我插入新的事件时,我只需要知道我刚刚创建的日历条目的ID,当我创建元素时,我也有它的ID。如果您需要在另一个时间更新它们,则最好在应用程序中保存对日历/事件项的引用(使用sqlite或其他方式)。 - Ivan
你正在获取MAX(id),因此我假设你得到了最后添加到日历中的事件。但是,你怎么知道用户没有取消你使用startActivity(intent)打开的日历活动呢? - dowi
好的,我已经有一段时间没有处理这个东西了,我几乎忘记了我做了什么。我将使用另一段代码更新我的答案,以解释getNextEventId的含义,但简要地说,我使用getNextEventId在事件表中创建事件条目,但不是真正的日历条目,它将由日历意图创建,我不记得为什么这样做:D - Ivan
请注意,使用getNewEventId(max_val+1)时要小心...请考虑用户是否已删除了最后一个事件...如果有x个记录与该ID相关(如余项或参与者),则添加新事件可能会导致意外结果。 - Maher Abuthraa
@SagarNayak 不幸的是,也许你可以检查一下他们是否再次更改了日历URI?也许它在不同的URI下? - Ivan

0

在插入事件后,您可以轻松地获取事件ID。

long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
//
// ... do something with event ID
//
//

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接