2012-02-28 304 views
7

我正在写一个应用程序,需要添加一些事件到android日历。对于插入我只是用下面的代码:Android日历,获取事件ID

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保存到我的应用程序,用户可以直接从应用程序内部调用该事件。 这可能吗?

+0

你确定你要的结束时间设置为当前时间? – toto2 2012-02-28 13:41:31

+0

ops:D nono!只是我的注意力分散:D – Ivan 2012-02-28 13:49:51

+0

如果您正在开发ICS,则有一个日历的新API;看到这个[博客](http://android-developers.blogspot.com/2011/10/ics-and-non-public-apis.html)。 – toto2 2012-02-28 13:55:01

回答

10

我提取了用于存储事件到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] “guestsCanSeeGuests”(ID = 830007843376)
[10] “exrule”(ID = 830007843464)
[11] “的rdate”(ID = 830007843528)
[12] “透明性”(ID = 830007843592 )
[13] “时区”(ID = 830007843672)
[14] “中选择了”(ID = 830007843744)
[15] “DTSTART”(ID = 830007843816) [16] “标题”(ID = 830007843888)
[17] “_sync_time”(ID = 830007843952)
[18] “_id”(ID = 830007844024) [19] “hasAttendeeData”(ID = 830007844088) [20]“_sync_i d “(ID = 830007844176)
[21] ”commentsUri“(ID = 830007844248) [22] ”的描述“(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; 
} 
+0

你怎么知道最后一个事件是你的用户添加的?也许这是一个更老的事件? (用户取消) – dowi 2015-02-19 13:25:06

+0

为什么我需要了解最后一个事件?当我插入一个新事件时,我只需要知道我刚刚创建的id日历条目,并且在创建元素时,我也有它的ID。如果你需要在另一时刻更新它们,可能最好在应用程序中保存对日历/事件项目的引用(使用sqlite或其他)。 – Ivan 2015-02-19 13:38:13

+0

你正在获得MAX(id),所以我假设你得到了最后一次加入日历的事件。但你怎么知道用户没有取消用startActivity(intent)打开的日历活动? – dowi 2015-02-19 13:45:57