在Android事件创建意图中指定日历 [英] Specify calendar in Android event creation intent

查看:106
本文介绍了在Android事件创建意图中指定日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过以下代码触发创建日历事件:

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

我尝试提供ACCOUNT_TYPEACCOUNT_NAMECALENDAR_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?

推荐答案

我相信,没有可靠的方法仅凭Intent来实现这一目标.您可能会遇到各种可能或可能不支持此功能的日历应用程序.

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:

只需在您想要的日历中创建一个占位符"事件(就像Android

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

编辑Intent可能不会返回状态来告诉您事件是否已保存,或者用户是否在未保存的情况下关闭了编辑器.因此,您应该读回该事件,并检查该事件是否已被用户修改,如果未更改,则将其删除.

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.

这篇关于在Android事件创建意图中指定日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆