添加日历活动而不打开日历

时间:2013-05-10 00:54:41

标签: java android android-intent android-calendar

在我的应用中,用户可以为任务设置最多3个提醒,但每次按“设置提醒”按钮,它都会打开日历应用。有没有办法在不打开默认日历应用程序的情况下设置日历事件?我只是想在不启动日历活动的情况下添加事件。

这就是我现在的代码:

Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, Calendar.MAY, 10, 3, 00);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, Calendar.MAY, 10, 4, 00);
endMillis = endTime.getTimeInMillis();

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(Events.TITLE, "Test Android");
intent.putExtra(Events.EVENT_LOCATION, "Test Location");
intent.putExtra(Events.DESCRIPTION, "Test Description Examples");

intent.putExtra(Events.DTSTART, startMillis);
intent.putExtra(Events.DTEND, endMillis);
intent.putExtra(Events.ALL_DAY, false);
intent.putExtra(Events.EVENT_END_TIMEZONE, "Europe/London");


intent.putExtra(Events.ACCESS_LEVEL, Events.ACCESS_PRIVATE);
intent.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

1 个答案:

答案 0 :(得分:8)

这看起来像这样:

    final ContentValues event = new ContentValues();
    event.put(Events.CALENDAR_ID, 1);

    event.put(Events.TITLE, title);
    event.put(Events.DESCRIPTION, description);
    event.put(Events.EVENT_LOCATION, location);

    event.put(Events.DTSTART, startTimeMillis);
    event.put(Events.DTEND, endTimeMillis);
    event.put(Events.ALL_DAY, 0);   // 0 for false, 1 for true
    event.put(Events.HAS_ALARM, 1); // 0 for false, 1 for true

    String timeZone = TimeZone.getDefault().getID();
    event.put(Events.EVENT_TIMEZONE, timeZone);

    Uri baseUri;
    if (Build.VERSION.SDK_INT >= 8) {
        baseUri = Uri.parse("content://com.android.calendar/events");
    } else {
        baseUri = Uri.parse("content://calendar/events");
    }

    context.getContentResolver().insert(baseUri, event);