2012-06-13 87 views
4

我只是試圖從我的Activity啓動Calendar Activity。我在我的Button OnClickListener下面的代碼:啓動日曆活動

Intent calIntent = new Intent(Intent.ACTION_INSERT); 
calIntent.setData(Uri.parse("content://com.android.calendar/events/")); 
startActivity(calIntent); 

但是當我點擊Button,我的設備掛起,然後我不得不強行關閉沒有響應的應用程序。

+0

的Android版本您使用的當月壓延看法?是ICS還是更新? – McGarnagle

+0

Android 4.0 API級別14 –

回答

1

這是因爲Calendar內容Uri因Android的不同版本(API級別)而異。嘗試使用此代碼獲取相應API級別的日曆Uri。

/* 
    * Determines if it's a pre 2.1 or a 2.2 calendar Uri, and returns the Uri 
    */ 
private String getCalendarUriBase(Context con) { 
    String calendarUriBase = null; 
    Uri calendars = Uri.parse("content://calendar/calendars"); 
    Cursor managedCursor = null; 
    try { 
     managedCursor = managedQuery(calendars, null, null, null, null); 
    } catch (Exception e) { 
     // eat 
    } 

    if (managedCursor != null) { 
     calendarUriBase = "content://calendar/"; 
    } else { 
     calendars = Uri.parse("content://com.android.calendar/calendars"); 
     try { 
      managedCursor = managedQuery(calendars, null, null, null, null); 
     } catch (Exception e) { 
      // statement to print the stacktrace 
     } 

     if (managedCursor != null) { 
      calendarUriBase = "content://com.android.calendar/"; 
     } 

    } 

    return calendarUriBase; 
} 
0

下面是爲我工作的代碼(使用> V4):

Uri uri = Uri.parse("content://com.android.calendar/events"); 
Intent calIntent = new Intent("android.intent.action.INSERT", uri) 
    .setAction(Intent.ACTION_INSERT); 
startActivity(calIntent); 
1

這是我個人CalendarOrganizer類,他們改變了日曆如何能冰之前,從冰淇淋三明治訪問,其實奶油三明治,建議使用他們的在線服務更新日曆,因爲谷歌日曆可能會更改或甚至沒有安裝。

編輯: 我才知道,我需要處理的意圖問題,而且,關於冰淇淋三明治某些手機將從Intent.ACTION_INSERT崩潰,而不是從Intent.ACTION_EDIT。爲此我更新了我的實施。感謝this post提供解決方案。

import android.content.Context; 
import android.content.Intent; 
import android.provider.CalendarContract; 
import android.provider.CalendarContract.Events; 

public class CalendarOrganizer { 
    private final static int ICE_CREAM_BUILD_ID = 14; 
    /** 
    * Creates a calendar intent going from startTime to endTime 
    * @param startTime 
    * @param endTime 
    * @param context 
    * @return true if the intent can be handled and was started, 
    * false if the intent can't be handled 
    */ 
    public static boolean createEvent(long startTime, long endTime, String title, String description, 
      String location, boolean isAllDay, Context context) { 
     int sdk = android.os.Build.VERSION.SDK_INT; 
     if(sdk < ICE_CREAM_BUILD_ID) { 
      // all SDK below ice cream sandwich 
      Intent intent = new Intent(Intent.ACTION_EDIT); 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra("beginTime", startTime); 
      intent.putExtra("endTime", endTime); 
      intent.putExtra("title", title); 
      intent.putExtra("description", description); 
      intent.putExtra("eventLocation", location); 
      intent.putExtra("allDay", isAllDay); 
//   intent.putExtra("rrule", "FREQ=YEARLY"); 

      try { 
       context.startActivity(intent); 
       return true; 
      } catch(Exception e) { 
       return false; 
      } 
     } else { 
      // ice cream sandwich and above 
      Intent intent = new Intent(Intent.ACTION_EDIT); 
      intent.setType("vnd.android.cursor.item/event"); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime); 
      intent.putExtra(Events.TITLE, title); 
      intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE); 
      intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY , isAllDay); 
      intent.putExtra(Events.DESCRIPTION, description); 
      intent.putExtra(Events.EVENT_LOCATION, location); 

//   intent.putExtra(Events.RRULE, "FREQ=DAILY;COUNT=10") 
      try { 
       context.startActivity(intent); 
       return true; 
      } catch(Exception e) { 
       return false; 
      } 
     } 
    } 
} 
0

下面是爲我工作

無需在清單創建活動的代碼。

適用於所有的Android平臺。

會告訴你

  long epoch = new Date.getTime(); 
      Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon(); 
      builder.appendPath("time"); 
      ContentUris.appendId(builder, epoch); 
      Intent intent = new Intent(Intent.ACTION_VIEW) 
        .setData(builder.build()); 
      startActivity(intent);