2011-03-20 41 views
5

有很多關於如何在android中創建新日曆事件的例子,但沒有關於如何打開和顯示事件的例子。這是到目前爲止我的代碼在android中打開並顯示日曆事件

public static void startCalendarMimeType(Context context, CalendarItem item){ 
    //all version of android 
    Intent i = new Intent(); 

    // mimeType will popup the chooser any for any implementing application (e.g. the built in calendar or applications such as "Business calendar" 
    i.setType("vnd.android.cursor.item/event"); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    // the time the event should start in millis. This example uses now as the start time and ends in 1 hour 
    //i.putExtra("beginTime", item.getBegin()); 
    //i.putExtra("endTime", item.getEnd()); 
    i.putExtra("_id", item.getId()); 


    // the action 
    //i.setAction(Intent.ACTION_PICK); 
    context.startActivity(i); 
} 

日曆項目包含使用內容解析器日曆已經檢索到的信息。當用戶點擊我的物品時,我希望它打開顯示物品的Android日曆。

在這一點上,你可以選擇一個應用程序打開,如果你選擇「顯示事件」,它會打開日曆應用程序,但得到一個空指針異常,我只是無法弄清楚我在這裏做錯了什麼。我是第一個嘗試這樣做的人?

任何幫助非常感謝

回答

2

有關於如何創造這些「榜樣」的Android

無新的日曆事件的例子很多應該使用,因爲沒有記錄並支持Android中的日曆API。

,但沒有就如何打開並顯示事件

您需要聯繫的任何第三方日曆程序的您要集成並要求他們怎麼做整合作者。如果您嘗試與作爲Android開放源代碼項目一部分的日曆應用程序集成,則該應用程序沒有文檔化和受支持的API。

+0

是真的,但上面的例子對編輯事件有效,但是當我只想查看一個項目時,有些如何給出空指針。這可以做到,只是不知道如何...。 – Alex 2011-03-22 06:30:06

11

我終於找到了解決辦法:

Intent intent = new Intent(Intent.ACTION_VIEW); 
//Android 2.2+ 
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID))); 
//Android 2.1 and below. 
//intent.setData(Uri.parse("content://calendar/events/" + String.valueOf(calendarEventID)));  
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
     | Intent.FLAG_ACTIVITY_SINGLE_TOP 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP 
     | Intent.FLAG_ACTIVITY_NO_HISTORY 
     | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
context.startActivity(intent); 

我希望,你們當中有些人覺得這有用。

我還增加了以下幾個其他日曆意圖:

/** 
* Add a calendar event. 
*/ 
private void addCalendarEvent(){ 
    Context context = getContext(); 
    Intent intent = new Intent(Intent.ACTION_EDIT); 
    intent.setType("vnd.android.cursor.item/event"); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP 
      | Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_NO_HISTORY 
      | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
    context.startActivity(intent); 
} 

/** 
* Edit a calendar event. 
*/ 
private void editCalendarEvent(){ 
    Context context = getContext(); 
    long calendarEventID = ..... 
    intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID))); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
     | Intent.FLAG_ACTIVITY_SINGLE_TOP 
     | Intent.FLAG_ACTIVITY_CLEAR_TOP 
     | Intent.FLAG_ACTIVITY_NO_HISTORY 
     | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
    context.startActivity(intent); 
} 

讓我知道如果任何人有任何問題或有更好的方法來完成相同的任務。

+0

偉大的答案男人。但是,當我運行代碼查看事件時,我的日期似乎總是在1969年12月回來。您是否有任何想法可能會導致這種情況? – 2012-11-22 22:16:42

+0

用於編輯您使用calendarEventID,但您不會在創建事件時如何獲取該ID – 2016-04-28 10:49:44

+0

由於'FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET'已棄用,我們可以使用'FLAG_ACTIVITY_NEW_DOCUMENT'參考:https://stackoverflow.com/a/33179077/2462531 – 2017-07-04 07:28:18