Intent for calendar doesn't get added to the calendar after saving

Viewed 39

This is the code I'm using to insert all the details into a google calendar app popup. Everything is working (right date, title, description) except if you click on "save" on the calendar popup it says: event saved successfully but the event doesn't actually get saved in the calendar.

        addToCalendar.setOnClickListener(v -> {

            Calendar beginTime = Calendar.getInstance();
            beginTime.set(datumvonCalendar.getJahr(),datumvonCalendar.getMonat(),datumvonCalendar.getTag(),zeitvonCalendar.getStunde(),zeitvonCalendar.getMinute(),0);

            Calendar endTime = Calendar.getInstance();
            endTime.set(datumbisCalendar.getJahr(),datumbisCalendar.getMonat(),datumbisCalendar.getTag(),zeitbisCalendar.getStunde(),zeitbisCalendar.getMinute(),0);

            Long millisEnd = endTime.getTimeInMillis();
            Long millisStart = beginTime.getTimeInMillis();

            Intent intent = new Intent(Intent.ACTION_INSERT);
            intent.setData(CalendarContract.Events.CONTENT_URI);
            intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,millisStart);
            intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,millisEnd);
            intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE,"Europe/Berlin");
            intent.putExtra(CalendarContract.Events.TITLE,headerTxt.getText().toString());
            intent.putExtra(CalendarContract.Events.DESCRIPTION,beschreibungTxt.getText().toString());
            fragment.startActivity(intent);
        });
1 Answers

SUGGESTION:

It seems like your code does not really have any issues, perhaps you can try this method:

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(datumvonCalendar.getJahr(),datumvonCalendar.getMonat(),datumvonCalendar.getTag(),zeitvonCalendar.getStunde(),zeitvonCalendar.getMinute(),0);

    Calendar endTime = Calendar.getInstance();
    endTime.set(datumbisCalendar.getJahr(),datumbisCalendar.getMonat(),datumbisCalendar.getTag(),zeitbisCalendar.getStunde(),zeitbisCalendar.getMinute(),0);

    Long millisEnd = endTime.getTimeInMillis();
    Long millisStart = beginTime.getTimeInMillis();

    Intent intent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,millisStart);
    intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,millisEnd);
    intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE,"Europe/Berlin");
    intent.putExtra(CalendarContract.Events.TITLE,headerTxt.getText().toString());
    intent.putExtra(CalendarContract.Events.DESCRIPTION,beschreibungTxt.getText().toString());
    fragment.startActivity(intent);

Also, you may want to make sure that your Begin and End time were setup properly.

Reference:

Related