简体   繁体   中英

Android - Adding a calendar event without reminder

I'm using the following function to add an event to calendar:

public String addEventToCalendar(long startDate, long endDate, String recurrenceRule, boolean isAllDay, String title, String description, String location, long calendarID) {
    ContentResolver cr = context.getContentResolver();
    ContentValues values = new ContentValues();
    TimeZone timeZone = TimeZone.getDefault();
    values.put(CalendarContract.Events.DTSTART, startDate);
    values.put(CalendarContract.Events.DTEND, endDate);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
    if (recurrenceRule != null)
        values.put(CalendarContract.Events.RRULE, recurrenceRule);
    values.put(CalendarContract.Events.TITLE, title);
    values.put(CalendarContract.Events.DESCRIPTION, description);
    values.put(CalendarContract.Events.CALENDAR_ID, calendarID);
    values.put(CalendarContract.Events.ALL_DAY, isAllDay);
    values.put(CalendarContract.Events.EVENT_LOCATION, location);
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        return null; // we don't have the right permissions
    }
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
    String eventID = uri.getLastPathSegment();
    return eventID;
}

It works, but the resulting calendar event, have a 30 minutes reminder! I'm not able to figure out why. Any clue please? Thanks a lot.

It seems like a default reminder is added after you insert your calendar event.

What you could try is to check if there are any reminder associated with your event right after you inserted it. And delete it if so.

CalendarContract.Reminders.query(contentResolver, eventId, projection)

will give you a list of reminder associated with the eventId

If the Cursor contains any reminder you can delete it with :

getContentResolver().delete(ContentUris.withAppendedId(CalendarContract.Reminders.CONTENT_URI, reminderId), null, null);

docs : http://developer.android.com/reference/android/provider/CalendarContract.Reminders.html

Try to add this

ContentValues values = new ContentValues();
values.put(CalendarContract.Events.HAS_ALARM, 0);

Whether the event has an alarm or not. Column name.

Type: INTEGER (boolean)

public static final String HAS_ALARM = "hasAlarm";

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