Manu George
Manu George

Reputation: 251

How to launch Android Calendar application using Intent (Froyo)

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.

Upvotes: 21

Views: 40738

Answers (7)

Ahmet B.
Ahmet B.

Reputation: 1674

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

Upvotes: 2

DjP
DjP

Reputation: 4577

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

Upvotes: 10

AlexAndro
AlexAndro

Reputation: 1928

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

Upvotes: 3

securelpb
securelpb

Reputation: 511

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

Upvotes: 3

roflharrison
roflharrison

Reputation: 2320

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.

Upvotes: 1

Muhammad Shahab
Muhammad Shahab

Reputation: 4270

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.

Upvotes: 0

Muhammad Shahab
Muhammad Shahab

Reputation: 4270

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

Upvotes: 24

Related Questions