gpt4 book ai didi

android - 如何使用事件 URI 以编程方式向日历事件添加提醒?

转载 作者:行者123 更新时间:2023-12-05 00:00:09 27 4
gpt4 key购买 nike

我正在尝试弄清楚如何在我的 Android 应用程序中向日历事件添加提醒。下面用红色圈出的图像是我想以编程方式添加到图像下方的代码中的内容。这就是我遇到的困难。我将不胜感激!

enter image description here

这就是我目前所拥有的...除了我需要帮助的部分外,一切正常。

 public void addEventToCalendar(String EventTitle, String EventDescription, String EventLocation, int EventStartDateYear, int EventStartDateMonth, int EventStartDateDay, int EventEndDateYear, int EventEndDateMonth, int EventEndDateDay, int BeginHour, int BeginMin, int EndHour, int EndMin, Boolean AllDay, String EventFrequency) {
Toast.makeText(this, "Adding Event To Your Calendar...", Toast.LENGTH_SHORT).show();
ContentValues event = new ContentValues();
Calendar startcalendar = Calendar.getInstance();
Calendar endcalendar = Calendar.getInstance();
startcalendar.set(EventStartDateYear, EventStartDateMonth, EventStartDateDay, BeginHour, BeginMin);
endcalendar.set(EventStartDateYear, EventEndDateMonth, EventEndDateDay, EndHour, EndMin);
event.put(CalendarContract.Events.CALENDAR_ID, 1);
event.put(CalendarContract.Events.TITLE, EventTitle);
event.put(CalendarContract.Events.DESCRIPTION, EventDescription);
event.put(CalendarContract.Events.EVENT_LOCATION, EventLocation);
event.put(CalendarContract.Events.DTSTART, startcalendar.getTimeInMillis());
event.put(CalendarContract.Events.DTEND, endcalendar.getTimeInMillis());
event.put(CalendarContract.Events.ALL_DAY, AllDay);
event.put(CalendarContract.Reminders.MINUTES, 5); //this here is what I am trying to figure out... 5 minutes before event or 5 hours before event, or 5 days before event, etc.
event.put(CalendarContract.Events.HAS_ALARM, true);
event.put(CalendarContract.Events.RRULE, EventFrequency);
event.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:00");
if (android.os.Build.VERSION.SDK_INT <= 7) {
eventsUri = Uri.parse("content://calendar/events");
} else {
eventsUri = Uri.parse("content://com.android.calendar/events");
}
Uri url = getContentResolver().insert(eventsUri, event);
Toast.makeText(this, "Event Added To Your Calendar!", Toast.LENGTH_SHORT).show();
}
}

最佳答案

您需要在 CalendarContract.Reminders 中插入一个条目,例如

public void addEventToCalendar(String EventTitle, String EventDescription, String EventLocation, int EventStartDateYear, int EventStartDateMonth, int EventStartDateDay, int EventEndDateYear, int EventEndDateMonth, int EventEndDateDay, int BeginHour, int BeginMin, int EndHour, int EndMin, Boolean AllDay, String EventFrequency) {
Toast.makeText(this, "Adding Event To Your Calendar...", Toast.LENGTH_SHORT).show();
ContentValues event = new ContentValues();
Calendar startcalendar = Calendar.getInstance();
Calendar endcalendar = Calendar.getInstance();
startcalendar.set(EventStartDateYear, EventStartDateMonth, EventStartDateDay, BeginHour, BeginMin);
endcalendar.set(EventStartDateYear, EventEndDateMonth, EventEndDateDay, EndHour, EndMin);
event.put(CalendarContract.Events.CALENDAR_ID, 1);
event.put(CalendarContract.Events.TITLE, EventTitle);
event.put(CalendarContract.Events.DESCRIPTION, EventDescription);
event.put(CalendarContract.Events.EVENT_LOCATION, EventLocation);
event.put(CalendarContract.Events.DTSTART, startcalendar.getTimeInMillis());
event.put(CalendarContract.Events.DTEND, endcalendar.getTimeInMillis());
event.put(CalendarContract.Events.ALL_DAY, AllDay);
event.put(CalendarContract.Events.HAS_ALARM, true);
event.put(CalendarContract.Events.RRULE, EventFrequency);
event.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:00");

Uri url = getContentResolver().insert(CalendarContract.Events.CONTENT_URI, event);

long eventId = Long.parseLong(url.getLastPathSegment());

ContentValues reminder = new ContentValues();
reminder.put(CalendarContract.Reminders.EVENT_ID, eventId);
reminder.put(CalendarContract.Reminders.MINUTES, 10);
reminder.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI, reminder);

Toast.makeText(this, "Event Added To Your Calendar!", Toast.LENGTH_SHORT).show();
}

关于android - 如何使用事件 URI 以编程方式向日历事件添加提醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com