Specify calendar in Android event creation intent

2019-05-23 22:31发布

I trigger the creation of a calendar event through the following piece of code:

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);

But I would like to specifiy which calendar should be used for the event creation (i.e set the initial value of the calendar dropdown in the event creation screen).

I've tried to provide ACCOUNT_TYPE, ACCOUNT_NAME and CALENDAR_ID

intent.putExtra(CalendarContract.Events.ACCOUNT_TYPE, accountType);
intent.putExtra(CalendarContract.Events.ACCOUNT_NAME, accountName);
intent.putExtra(CalendarContract.Events.CALENDAR_ID, calendarId);

but it has no effect on the calendar dropdown initial value.

Is it possible to specify a calendar in the event creation intent?

3条回答
Anthone
2楼-- · 2019-05-23 23:12

I believe, there is no reliable way to achieve this with just an Intent. You may come across all sorts of calendar apps that may or may not support this.

If you don't mind if the user can not change the calendar you can achieve this with a simple trick:

Just create a "placeholder" event in the calendar you want to (like outlined in the Android Developer Guide) and fire an Intent.ACTION_EDIT Intent for this event instead.

Here is some (untested) code to show how this could work:

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();

// provide an initial time
long now=System.currentTimeMillis();
values.put(Events.DTSTART, now);
values.put(Events.DTEND, now+3600000);
values.put(Events.TITLE, "");
values.put(Events.CALENDAR_ID, calendarId);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

// create the "placeholder" event
Uri uri = cr.insert(Events.CONTENT_URI, values);

// get the event ID that is the last element in the Uri
long eventId = Long.parseLong(uri.getLastPathSegment());

// launch the editor to edit the "placeholder" event
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId));
startActivityForResult(intent, 0);

The edit Intent probably doesn't return a status that tells you if the event has been saved or if the user dismissed the editor without saving. So you should read back the event and check if it has been modified by the user and delete it if it has not been changed.

查看更多
一夜七次
3楼-- · 2019-05-23 23:16

Its's Simple to add Event Using this simple Method.

public void onAddEventClicked(View view) {
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType("vnd.android.cursor.item/event");

        Calendar cal = Calendar.getInstance();
        long startTime = cal.getTimeInMillis();
        long endTime = cal.getTimeInMillis() + 60 * 60 * 1000;

        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime); 
        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);

        intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);

        //This is Information about Calender Event.
        intent.putExtra(Events.TITLE, "Siddharth Birthday");
        intent.putExtra(Events.DESCRIPTION, "This is a description");
        intent.putExtra(Events.EVENT_LOCATION, "My Guest House");
        intent.putExtra(Events.RRULE, "FREQ=YEARLY");

        startActivity(intent);
    }
查看更多
Fickle 薄情
4楼-- · 2019-05-23 23:27

Use the following code snippet to invoke the default calendar event.

Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType("vnd.android.cursor.item/event");
startActivity(intent);

The following code bundle can be used to pass the information to calendar intent.

intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime);
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
intent.putExtra(Events.TITLE, "Neel Birthday");
intent.putExtra(Events.DESCRIPTION, "This is a sample description");
intent.putExtra(Events.EVENT_LOCATION, "My Guest House");
intent.putExtra(Events.RRULE, "FREQ=YEARLY");

May it will help you :) Thanks.

Source: How to Add an Event to Device Calendar in Android

You can check this one too. Adding Events to the User’s Calendar

查看更多
登录 后发表回答