sda*_*bet 2 android android-intent android-calendar
我通过以下代码触发日历事件的创建:
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
但我想指定应该使用哪个日历来创建事件(即在事件创建屏幕中设置日历下拉列表的初始值)。
我试图提供ACCOUNT_TYPE,ACCOUNT_NAME并且CALENDAR_ID
intent.putExtra(CalendarContract.Events.ACCOUNT_TYPE, accountType);
intent.putExtra(CalendarContract.Events.ACCOUNT_NAME, accountName);
intent.putExtra(CalendarContract.Events.CALENDAR_ID, calendarId);
Run Code Online (Sandbox Code Playgroud)
但它对日历下拉初始值没有影响。
是否可以在事件创建意图中指定日历?
我相信,没有可靠的方法可以仅通过Intent. 您可能会遇到各种可能支持也可能不支持的日历应用程序。
如果您不介意用户无法更改日历,您可以通过一个简单的技巧来实现:
只需在您想要的日历中创建一个“占位符”事件(如 Android开发人员指南中所述),然后Intent.ACTION_EDIT Intent为该事件触发一个。
下面是一些(未经测试的)代码来展示它是如何工作的:
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);
Run Code Online (Sandbox Code Playgroud)
编辑Intent可能不会返回一个状态,告诉您事件是否已保存,或者用户是否在没有保存的情况下关闭了编辑器。因此,您应该回读该事件并检查它是否已被用户修改,如果未更改则将其删除。
| 归档时间: |
|
| 查看次数: |
3291 次 |
| 最近记录: |