简体   繁体   中英

Android Insert Calendar Intent - Without alarm/reminder

I want to insert a calendar event via intent. But the "add event"-Activity should not be prefilled with a reminder/alarm.

Intent intent = new Intent(Intent.ACTION_INSERT)
    .setData(Events.CONTENT_URI)
    .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
    .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
    .putExtra(Events.TITLE, title)
    .putExtra(Events.DESCRIPTION, description)
    .putExtra(Events.HAS_ALARM, false)
    .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);

This intent will start the calendar's "add event"-activity prefilled with some data. However, although I set Events.HAS_ALARM to false, the activity is pre-populated with a reminder (tested on Android ICS).

What's even worse, the reminder is prepopulated to 10 minutes before the event, which in case of an all-day event is really bad. Who wants to be reminded at 11.50 pm of an event the next day?

What I am missing here?

I never tried your technique above. Here is the code snippet I use to save Calendar.

public static void saveCalendar(Context ctx, String title,
        String description, String location, Calendar cal_start,
        Calendar cal_end) {

    // look for calendar
    Cursor cursor = ctx.getContentResolver()
            .query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "displayname" }, "selected=1",
                    null, null);
    cursor.moveToFirst();
    String[] CalNames = new String[cursor.getCount()];
    int[] CalIds = new int[cursor.getCount()];
    for (int i = 0; i < CalNames.length; i++) {
        CalIds[i] = cursor.getInt(0);
        CalNames[i] = cursor.getString(1);
        cursor.moveToNext();
    }

    cursor.close();

    // put calendar event
    ContentValues event = new ContentValues();
    event.put("calendar_id", CalIds[0]);
    event.put("title", title);
    event.put("description", description);
    event.put("eventLocation", location);
    event.put("dtstart", cal_start.getTimeInMillis());
    event.put("dtend", cal_end.getTimeInMillis());
    event.put("hasAlarm", 1);

    Uri eventsUri = Uri.parse("content://com.android.calendar/events");
    Uri newEvent = ctx.getContentResolver().insert(eventsUri, event);

    // put alarm reminder for an event, 2 hours prior
    long eventID = Long.parseLong(newEvent.getLastPathSegment());

    ContentValues cv_alarm = new ContentValues();
    cv_alarm.put("event_id", eventID);
    cv_alarm.put("method", 1);
    cv_alarm.put("minutes", 120);
    ctx.getContentResolver()
            .insert(Uri.parse("content://com.android.calendar/reminders"),
                    cv_alarm);

}

It you don't want the alarm/reminder set 0 to hasAlarm and don't put the codes to add the alarm. It works for me.

HAS_ALARM列期望Integer布尔值为0或1。

intent.putExtra(Events.HAS_ALARM, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM