Android日历,获取事件ID

时间:2012-02-28 13:09:28

标签: java android android-calendar

我正在编写一个需要向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保存到我的应用程序中后,用户可以直接从应用程序内部调用该事件。 可能吗?

2 个答案:

答案 0 :(得分:12)

我将用于存储事件的列列表提取到android日历中。 列表如下:

[0]“originalEvent”(id = 830007842672)
[1]“availabilityStatus”(id = 830007842752)
[2]“ownerAccount”(id = 830007842840)
[3]“_sync_account_type”(id = 830007842920)
[4]“能见度”(id = 830007843008)
[5]“rrule”(id = 830007843080)
[6]“lastDate”(id = 830007843144)
[7]“hasAlarm”(id = 830007843216)
[8]“guestsCanModify”(id = 830007843288) [9]“客人可以参观”(id = 830007843376)
[10]“exrule”(id = 830007843464)
[11]“rdate”(id = 830007843528)
[12]“透明度”(id = 830007843592)
[13]“时区”(id = 830007843672)
[14]“选中”(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;
}

插入事件的ANd:

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

2015年9月更新

根据评论中的要求,我添加了如何获取日历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;
}

答案 1 :(得分: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
//
//