How to use Calendar Intent?

2019-01-26 15:36发布

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

7条回答
可以哭但决不认输i
2楼-- · 2019-01-26 16:04

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));
查看更多
Emotional °昔
3楼-- · 2019-01-26 16:08

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.

查看更多
Evening l夕情丶
4楼-- · 2019-01-26 16:12

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)
查看更多
Lonely孤独者°
5楼-- · 2019-01-26 16:16

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.

查看更多
我只想做你的唯一
6楼-- · 2019-01-26 16:20

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>
查看更多
叛逆
7楼-- · 2019-01-26 16:20

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 );
查看更多
登录 后发表回答