我知道这个论坛之前已经问过这个问题,但是我尝试了这里写的所有内容,但仍然没有任何效果。我想要做的是在日历中插入一个事件。由于我的设备上有多个日历应用程序,我想让用户选择哪个日历应用程序应该包含新事件,类似于用户尝试使用地图应用程序查看位置时发生的情况(用户可以选择是否激活谷歌地图,互联网,...)。因此,我必须使用 Intent。
顺便说一句,我知道只有在 SDK 版本 14 或更高版本的设备上才允许使用 Intents 将新事件插入日历。我的设备的 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
我究竟做错了什么?
谢谢