2012-02-28 94 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