将事件添加到Android日历而不显示日历

时间:2013-04-14 17:48:05

标签: android

我有这个代码用于添加事件:

Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);

Calendar beginTime = Calendar.getInstance();
beginTime.set(2013,04,14,21,00,00);

Calendar endTime = Calendar.getInstance();
endTime.set(2013,04,14,21,10,00);
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", beginTime.getTimeInMillis());
intent.putExtra("endTime", endTime.getTimeInMillis());
startActivity(intent);

但如何在不打开日历的情况下插入事件?

感谢

1 个答案:

答案 0 :(得分:1)

您需要使用ContentResolver代替Intent

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

Calendar beginTime = Calendar.getInstance();
beginTime.set(2013,04,14,21,00,00);

Calendar endTime = Calendar.getInstance();
endTime.set(2013,04,14,21,10,00);

values.put(Events.DTSTART, beginTime.getTimeInMillis());
values.put(Events.DTEND, endTime.getTimeInMillis());
values.put(Events.TITLE, "Some title");
values.put(Events.DESCRIPTION, "Some description");

Uri uri = cr.insert(Events.CONTENT_URI, values);

您可以阅读更多Calendar Provider