How to Launch Android Calendar Application Using Intent (Froyo)

I want to open up the Calendar application from an android application

Intent intent = new Intent(Intent.ACTION_EDIT);  

intent.setType("vnd.android.cursor.item/event");

intent.putExtra("title", "Some title");

intent.putExtra("description", "Some description");

intent.putExtra("beginTime", eventStartInMillis);

intent.putExtra("endTime", eventEndInMillis);

startActivity(intent);

how can i open the calendar from my app?

If you just want to open the calendar you can use and intent with EITHER of these component names (you might have to cater for both if you want to support older phones)

Intent i = new Intent();

//Froyo or greater (mind you I just tested this on CM7 and the less than froyo one worked so it depends on the phone...)
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");

//less than Froyo
cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");

i.setComponent(cn);
startActivity(i);

If you want to go to the add event screen (which sounds better for your purpose) use something like:

 //all version of android
Intent i = new Intent();

// mimeType will popup the chooser any for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
i.setType("vnd.android.cursor.item/event");

// the time the event should start in millis. This example uses now as the start time and ends in 1 hour
i.putExtra("beginTime", new Date().getTime());
i.putExtra("endTime", new Date().getTime() + DateUtils.HOUR_IN_MILLIS);

// the action
i.setAction(Intent.ACTION_EDIT);
startActivity(i);

(code is untested, copied from an existing project)

opening default calendar application from another app in android

After reviewing the Calendar App in the Android source code you can only invoke the AgendaActivity directly. The others will not work. You can interact directly with the cursor to read/create events, but you can't invoke the calendar app to a view other than the AgendaView. The reason is that the developers have limited that ability in the manifest for the Cal app by using the following activity definitions:

 <activity android:name="MonthActivity" android:label="@string/month_view"
android:theme="@style/CalendarTheme" />
<activity android:name="WeekActivity" android:label="@string/week_view"
android:theme="@style/CalendarTheme" />
<activity android:label="@string/day_view" android:name="DayActivity"
android:theme="@style/CalendarTheme"/>
<activity android:name="AgendaActivity" android:label="@string/agenda_view"
android:theme="@android:style/Theme.Light"
android:exported="true" />

Note that only the AgendaActivity has android:exported="true". If you attempt to call the other activities you will get a permission exception.



Related Topics



Leave a reply



Submit