4

I am trying to Launch a calendar from my app, so I can let users put their appointments to it and then have my app read those.

I tried the piece of code I found on here:

ComponentName cn; 
Intent i = new Intent();         
cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
i.setComponent(cn);
startActivity(i);

and get the error

07-07 21:05:33.944: ERROR/AndroidRuntime(1089):
    Caused by: android.content.ActivityNotFoundException: Unable to find explicit
    activity class {com.google.android.calendar/com.android.calendar.LaunchActivity};
    have you declared this activity in your AndroidManifest.xml?

I did declare the com.android.calendar.LaunchActivity to my android manifest file and it still isn't working...

I'd think this not doable using the emulator? I am doing this in Android 3.0 (HoneyComb).

Sufian
  • 6,405
  • 16
  • 66
  • 120
Codejoy
  • 3,722
  • 13
  • 59
  • 99

7 Answers7

12

This, from Andriod docs, worked for me:

long startMillis = System.currentTimeMillis();
Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
ContentUris.appendId(builder, startMillis);
intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
startActivity(intent);

More info here: Android calendar docs.

apb
  • 413
  • 4
  • 8
  • In MonoDroid do Java.Lang.JavaSystem.CurrentTimeMillis() for millis. – samus Jan 14 '14 at 19:49
  • You actually don't need the millisecond ID. Just: `new Intent(Intent.ACTION_VIEW, CalendarContract.CONTENT_URI.buildUpon().appendPath("time").build());` – Robert Mar 01 '17 at 20:01
4

First, it will not work in the emulator, as the Calendar app that you are trying to use is not in the emulator image.

Second, this app may or may not be on any production device. Anyone creating an Intent using a ComponentName (pointing to somebody's else's component) is asking for trouble at best.

Third, you will not be able to "have my app read those" right away even if the user does elect to add an event. They will only be available via the Google Calendar GData API after a sync, which may not be immediate.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so if i want the user to be able to pick dates off a calendar or add events to a calendar I have to roll my own? Or is there like a premade piece of code (i hesitate to use the term widget) that would save me the pain of reinventing the wheel? – Codejoy Jul 08 '11 at 00:14
  • 1
    To pick a date, use `DatePicker`, `DatePickerDialog`, or `CalendarView` (latter new to Honeycomb). To add events to or retrieve data from a user's Google Calendar, use the Google Calendar GData API. Or, for your own calendar implementation, store the data in a database. To show a schedule of events, you might grab some of the code from the Google I|O 2011 app (`iosched`). Right now, your plan assumes all users are forced at gunpoint to have devices with a certain calendar app and to use that calendar app, and you're using undocumented/unsupported APIs to achieve that dubious end. – CommonsWare Jul 08 '11 at 00:37
  • Ahh okay. The CalendarView sounds promising, I will investigate the DatePickerDialog more and the iosched is totally new to me sounds interesting too. Thanks for the info. – Codejoy Jul 08 '11 at 03:01
3

The simplest way to open the Google Calendar without a given date:

Uri calendarUri = CalendarContract.CONTENT_URI
            .buildUpon()
            .appendPath("time")
            .build();
startActivity(new Intent(Intent.ACTION_VIEW, calendarUri));
Robert
  • 1,936
  • 27
  • 38
1

you must write this

setContentView(R.layout.tabla);
    ComponentName cn; 
    Intent i = new Intent();         
    cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");
    i.setComponent(cn);
    startActivity(i);   
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Jesus
  • 11
  • 1
0

If you are using API 14 or higher you can use this code

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent)
tron
  • 13
  • 2
0

You need to give the app permission to read the calendar. Try adding this to your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>
THE_DOM
  • 4,266
  • 1
  • 18
  • 18
-1

apb's answer translated to MonoDroid is

long startMillis = Java.Lang.JavaSystem.CurrentTimeMillis( );

Uri.Builder builder = Android.Provider.CalendarContract.ContentUri.BuildUpon( );
builder.AppendPath( "time" );

ContentUris.AppendId( builder,startMillis );

Intent intent = new Intent( Intent.ActionView );
intent.SetData( builder.Build() );

StartActivity( intent );
samus
  • 6,102
  • 6
  • 31
  • 69