我知道以前在这个论坛上曾经问过这个问题,但是我尝试了这里所写的所有内容,但对我来说仍然无效。我想要做的是在日历中插入一个事件。由于我的设备上有多个日历应用程序,我想让用户选择应该包含新事件的日历应用程序,类似于用户尝试使用地图应用程序查看位置时发生的情况(用户可以选择是否激活谷歌地图,互联网,...)。出于这个原因,我必须使用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
我做错了什么?
感谢名单
答案 0 :(得分:15)
您捕获了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));
您可以在开发人员文档中查看其他属性。
答案 1 :(得分:4)
插入像这样的事件
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, "rowan@example.com,trevor@example.com");
startActivity(intent);
答案 2 :(得分:0)
您可以使用此代码,因为它首先检查是否有可用应用可以处理此意图。
fun addToCalenderAction(ctx: Context, session: ItemSession) {
val intent = Intent(Intent.ACTION_INSERT).apply {
data = CalendarContract.Events.CONTENT_URI
putExtra(CalendarContract.Events.TITLE, session.title ?: "AA")
putExtra(CalendarContract.Events.EVENT_LOCATION, session.location
?: "Egypt, ALex, ST23")
putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, parseTimeOfFirebase(session.startTime
?: "2021-05-31T19:56:22.636+0000"))
putExtra(CalendarContract.EXTRA_EVENT_END_TIME, parseTimeOfFirebase(endTime
?: "2021-05-31T21:56:22.636+0000"))
}
if (intent.resolveActivity(ctx.packageManager) != null) {
ctx.startActivity(intent)
}
}
注意:session是保存一些数据的对象,你可以使用自己的数据