20

I want to open up the Calendar application from an android application. When i searched online, all i got is to create new events using intent. I could find Intents to open Contacts and Gallery etc.

Is it possible to launch the Calendar to a specific week or day? If possible, could someone please help me with it.

1
  • I tried, but it gave me a permission denied exception Apr 2, 2012 at 7:48

7 Answers 7

24
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);
3
  • when i tried this i got the following error..Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDIT typ=vnd.android.cursor.item/event (has extras) }..could you please give a solution on this.?
    – TKV
    Jan 5, 2012 at 12:10
  • 6
    @TijoKVarghese : Make sure you're running or debugging your app in a Android phone instead of emulator. I heard Android Calendar is not defaultly packed into emulator image.
    – Evi Song
    Jan 10, 2012 at 10:58
  • 1
    ok..i will check this with actual device..till now i had tested only on android emulator.
    – TKV
    Jan 10, 2012 at 11:23
10

You can open Calendar View by two means:

1) by particular date 2) by particular event

For this you have to add following two permissions

  <uses-permission android:name="android.permission.READ_CALENDAR" />
  <uses-permission android:name="android.permission.WRITE_CALENDAR" />

1) by particular date:

Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, Calendar.getInstance().getTimeInMillis());
Intent intent = new Intent(Intent.ACTION_VIEW)
    .setData(builder.build());
startActivity(intent);

2) by particular event:

long eventID = 200;

Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(uri);
startActivity(intent);

Note: CalendarContract content provider was added to the Android SDK in API Level 14. For more information you can visit this link

3

After reviewing the Calendar App in the Android source code you can only invoke the AgendaActivity directly. The others will not work. As the other posters have noted 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.

However, you can invoke the AgendaActivity to an arbitrary day with the following code:

    Calendar tempCal = (Calendar) mCalendar.clone();
    tempCal.set(year, month, day); 
    Intent calendarIntent = new Intent() ;
    calendarIntent.putExtra("beginTime", tempCal.getTimeInMillis());
    calendarIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");
    startActivity(calendarIntent);
3

Using intents to view calendar data

Calender Provider offers two different ways to use the VIEW Intent:

To open the Calendar to a particular date.
To view an event.

add permissions to manifest

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

Here is an example that shows how to open the Calendar to a particular date:

 // A date-time specified in milliseconds since the epoch. 
 long startMillis;
 ...
 Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();   
 builder.appendPath("time");
 ContentUris.appendId(builder, startMillis);
 Intent intent = new Intent(Intent.ACTION_VIEW)
     .setData(builder.build());
   startActivity(intent);

Here is an example that shows how to open an event for viewing:

long eventID = 208;
...
Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
Intent intent = new Intent(Intent.ACTION_VIEW)
   .setData(uri);

    startActivity(intent);

Is it possible to launch the Calendar to a specific week or day?

So, now it is possible, but requires min API 14.

For more details visit http://developer.android.com/guide/topics/providers/calendar-provider.html#intents

1
  • This works great on API => 14. Any idea if the view intent can handle other parameters, besides "time"? To open a time period (week, month) for example?
    – AndreasEK
    Jan 14, 2014 at 8:15
1

AgendaActivity loads the "Agenda" view.

From my experience you can't deep link into the Day, Week and Month activities in stock Android, however you can use "LaunchActivity" which loads the last opened view.

0

By a process of tedious experimentation, I've found that:

Intent calendarIntent = new Intent() ;
calendarIntent.setClassName("com.android.calendar","com.android.calendar.AgendaActivity");

...works for me to launch the Calendar's Agenda Activity. Haven't tried, but perhaps com.android.calendar.DayActivity, .WeekActivity,and .MonthActivity will launch the corresponding Activities.

0

You can use the below code, which also enables you to choose a calendar app if there are multiple calendar apps installed on your device

fun openCalendarApp(activity: Activity?) {
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_APP_CALENDAR)
    activity?.startActivity(intent)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.