How to launch Android Calendar application using Intent (Froyo)
Asked Answered
D

7

21

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.

Ding answered 7/12, 2010 at 3:16 Comment(1)
I tried, but it gave me a permission denied exceptionClasp
S
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);
Succuss answered 7/12, 2010 at 4:44 Comment(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.?Goodman
@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.Landy
ok..i will check this with actual device..till now i had tested only on android emulator.Goodman
R
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

Rentschler answered 12/12, 2014 at 8:5 Comment(0)
P
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);
Phrixus answered 20/2, 2011 at 19:54 Comment(0)
U
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

Unscathed answered 28/1, 2013 at 20:32 Comment(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?Puppy
F
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.

Flourish answered 7/12, 2010 at 4:52 Comment(0)
P
1

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)
}
Precritical answered 6/7, 2023 at 12:5 Comment(0)
S
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.

Succuss answered 7/12, 2010 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.