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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Manu George
  • 241
  • 1
  • 3
  • 4

7 Answers7

23
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);
Muhammad Shahab
  • 4,187
  • 4
  • 34
  • 44
  • 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 05 '12 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 '12 at 10:58
  • 1
    ok..i will check this with actual device..till now i had tested only on android emulator. – TKV Jan 10 '12 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

alexyorke
  • 4,224
  • 3
  • 34
  • 55
DjP
  • 4,537
  • 2
  • 25
  • 34
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

Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
AlexAndro
  • 1,918
  • 2
  • 27
  • 53
  • 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 '14 at 08:15
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);
securelpb
  • 511
  • 1
  • 5
  • 7
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.

roflharrison
  • 2,300
  • 23
  • 26
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.

Muhammad Shahab
  • 4,187
  • 4
  • 34
  • 44
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)
}
Ahmet B.
  • 1,290
  • 10
  • 20