Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert event to calendar using intent

Tags:

android

I know this has been asked previously in this forum, but I tried everything written here and still nothing works for me. What I want to do is to insert an event to the calendar. Since there are several calendar applications on my device, I want to enable the user to choose which calendar application should contain the new event, similar to what happens when the user attempts to view a location using a map application (the user can select whether to activate google maps, the internet, ...). For this reason, I have to use an Intent.

BTW, I'm aware that inserting new events to the calendar using Intents is allowed only on devices with SDK version 14 or higher. My device has an API level of 15, so it supports the calendar API.

Here's my code:

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);

I get this exception:

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

What am I doing wrong?

Thanx

like image 785
shai Avatar asked Feb 04 '13 20:02

shai


1 Answers

You caught ActivityNotFoundException because any activity can't handle your action.

Instead using Intent.ACTION_INSERT try to do like this :

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));

You can check in developer documentation for other attributes.

like image 178
Yvan RAJAONARIVONY Avatar answered Oct 19 '22 15:10

Yvan RAJAONARIVONY