2013-02-04 32 views
8

我知道這個問題以前在這個論壇上被問過,但我試過在這裏寫的所有東西,但仍然沒有爲我工作。我想要做的是將一個事件插入日曆。由於我的設備上有幾個日曆應用程序,因此我想讓用戶選擇哪個日曆應用程序應包含新事件,類似於用戶嘗試使用地圖應用程序查看位置時發生的情況(用戶可以選擇是激活谷歌地圖,互聯網,...)。出於這個原因,我必須使用意圖。使用意圖插入事件到日曆

順便說一句,我知道使用Intents將新事件插入日曆只允許在SDK版本爲14或更高的設備上使用。我的設備的API級別爲15,因此它支持日曆API。

這裏是我的代碼:

Intent calendarIntent = new Intent(Intent.ACTION_INSERT); 
calendarIntent.setData(Events.CONTENT_URI); 
calendarIntent.putExtra(Events.TITLE, "title"); 
calendarIntent.putExtra(Events.EVENT_LOCATION, "address"); 
Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.DAY_OF_MONTH, 1); 
cal.set(Calendar.MONTH, 0); 
cal.set(Calendar.YEAR, 2013); 
cal.set(Calendar.HOUR_OF_DAY, 20); 
cal.set(Calendar.MINUTE, 0); 
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, cal.getTime().getTime()); 
cal.set(Calendar.HOUR_OF_DAY, 20); 
cal.set(Calendar.MINUTE, 30); 
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, cal.getTime().getTime()); 
ctx.startActivity(calendarIntent); 

我得到這個異常:

02-04 17:55:23.957: E/AndroidRuntime(3781): FATAL EXCEPTION: main 
02-04 17:55:23.957: E/AndroidRuntime(3781): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.applicat.meuchedet/com.applicat.meuchedet.MainScreenActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.access$600(ActivityThread.java:127) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.os.Looper.loop(Looper.java:137) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.main(ActivityThread.java:4507) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at dalvik.system.NativeStart.main(Native Method) 
02-04 17:55:23.957: E/AndroidRuntime(3781): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.android.calendar/events (has extras) } 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1535) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1387) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Activity.startActivityForResult(Activity.java:3190) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.Screen.startActivity(Screen.java:433) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.CalendarAppointmentScheduler.writeAppointmentToCalendar(CalendarAppointmentScheduler.java:137) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at com.applicat.meuchedet.MainScreenActivity.onCreate(MainScreenActivity.java:258) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Activity.performCreate(Activity.java:4465) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932) 
02-04 17:55:23.957: E/AndroidRuntime(3781):  ... 11 more 

我在做什麼錯?

Thanx

+0

事件你已經安裝了日曆應用程序?錯誤表明沒有處理意圖的東西。 – BoredAndroidDeveloper

回答

11

您捕獲了ActivityNotFoundException,因爲任何活動都無法處理您的操作。

而不是使用Intent.ACTION_INSERT試着這樣做:

Intent intent = new Intent(Intent.ACTION_EDIT); 
intent.setType("vnd.android.cursor.item/event"); 
intent.putExtra(Events.TITLE, strTitle); 
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
        startDateMillis); 
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
        endDateMillis); 
intent.putExtra(Events.ALL_DAY, false);// periodicity 
      intent.putExtra(Events.DESCRIPTION,strDescription)); 

可以開發者文檔中檢查其他屬性。

+0

我認爲伊萬在看到這個後指出你正確的方向:http://stackoverflow.com/questions/9926830/error-inserting-event-into-calendar-with-intent顯然有些手機不尊重ACTION_INSERT和而是需要ACTION_EDIT。我不認爲你需要使用setType行,但我可能是錯的 – BoredAndroidDeveloper

+0

我總是使用Yvan的方法,它的工作原理。 – psykhi

+0

謝謝!這對我行得通。你知道爲什麼有必要使用setType代碼行嗎? – shai

0

插入這樣

Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2012, 0, 19, 7, 30); 
Calendar endTime = Calendar.getInstance(); 
endTime.set(2012, 0, 19, 8, 30); 
Intent intent = new Intent(Intent.ACTION_INSERT) 
    .setData(Events.CONTENT_URI) 
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
beginTime.getTimeInMillis()) 
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
endTime.getTimeInMillis()) 
    .putExtra(Events.TITLE, "Yoga") 
    .putExtra(Events.DESCRIPTION, "Group class") 
    .putExtra(Events.EVENT_LOCATION, "The gym") 
    .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY) 
    .putExtra(Intent.EXTRA_EMAIL, "[email protected],[email protected]"); 
startActivity(intent);