Marco Ferrante
Marco Ferrante

Reputation: 21

add programmatically calendar event on android 13 with java

I created a booking application. when a reservation is made, an event is automatically added to the calendar. this works on most devices. i came across some devices where it doesn't work (xiaomi and samsung) i don't know if the cause is the os version or something else.

the following code is what i use for most devices

ContentValues event = new ContentValues();
        event.put(CalendarContract.Events.CALENDAR_ID, 1);
        event.put(CalendarContract.Events.TITLE, "title");
        event.put(CalendarContract.Events.DESCRIPTION, "description");
        event.put(CalendarContract.Events.EVENT_LOCATION, "location");
        event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

        event.put(CalendarContract.Events.DTSTART, date + (begin * 3600000) + 1800000);
        event.put(CalendarContract.Events.DTEND, date + (end * 3600000) + 1800000);

        event.put(CalendarContract.Events.ALL_DAY, 0); // 0 per false, 1 per true
        event.put("eventStatus", 1);
        event.put(CalendarContract.Events.HAS_ALARM, 0);

        String eventUriString = "content://com.android.calendar/events";
        Uri eventUri = requireContext().getApplicationContext()
                .getContentResolver()
                .insert(Uri.parse(eventUriString), event);
        long eventID = Long.parseLong(eventUri.getLastPathSegment());


        int minutes=30;


        // add reminder for the event
        ContentValues reminders = new ContentValues();
        reminders.put("event_id", eventID);
        reminders.put("method", "1");
        reminders.put("minutes", minutes);

        String reminderUriString = "content://com.android.calendar/reminders";
        getContext().getContentResolver()
                .insert(Uri.parse(reminderUriString), reminders);

I found some code somewhere that allows you to open the calendar app with the fields already filled in. what I want, however, is that the user does not even have the perception that the event has been created

Upvotes: 2

Views: 264

Answers (0)

Related Questions