Add event to calendar API 7

2019-08-11 09:50发布

问题:

How can I add an event to calendar using a phone with API 7?

Below is my try, but it doesnt work:

Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(CalendarContract.Events.CONTENT_URI)
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begincalendar.getTimeInMillis())
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endcalendar.getTimeInMillis())
                .putExtra(CalendarContract.Events.TITLE, "test")
                .putExtra(CalendarContract.Events.DESCRIPTION, "describe")
                .putExtra(CalendarContract.Events.EVENT_LOCATION, "place")
                .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY,true);

startActivity(intent);

回答1:

Try the below solution.

Intent intent = new Intent(Intent.ACTION_INSERT)
                .setType("vnd.android.cursor.item/event")
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());

activity.startActivityForResult(intent, 100);


回答2:

Try this it will help ...

  Calendar beginTime = Calendar.getInstance();
        beginTime.set(yearInt, monthInt - 1, dayInt, 7, 30);



        ContentValues l_event = new ContentValues();
        l_event.put("calendar_id", CalIds[0]);
        l_event.put("title", "event");
        l_event.put("description",  "This is test event");
        l_event.put("eventLocation", "School");
        l_event.put("dtstart", beginTime.getTimeInMillis());
        l_event.put("dtend", beginTime.getTimeInMillis());
        l_event.put("allDay", 0);
        l_event.put("rrule", "FREQ=YEARLY");
        // status: 0~ tentative; 1~ confirmed; 2~ canceled
        // l_event.put("eventStatus", 1);

        l_event.put("eventTimezone", "India");
        Uri l_eventUri;
        if (Build.VERSION.SDK_INT >= 8) {
            l_eventUri = Uri.parse("content://com.android.calendar/events");
        } else {
            l_eventUri = Uri.parse("content://calendar/events");
        }
        Uri l_uri = MainActivity.this.getContentResolver()
                .insert(l_eventUri, l_event);