事件添加到日历API 7(Add event to calendar API 7)

2019-10-19 07:10发布

我如何添加一个事件使用手机API 7日历?

下面是我的尝试,但它不工作:

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);

Answer 1:

尝试以下解决方案。

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);


Answer 2:

试试这个它会帮助...

  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);


文章来源: Add event to calendar API 7